The other answers are already very good, and I know your example is just an example, but I want to point out a big part of this process that hasn't been discussed yet:
You need to identify your assumptions, and then test those assumptions against corner cases.
Looking at your example, I see a couple assumptions:
- The recursive approach will eventually cause an error.
- Nobody will see this error because videos take too long to play to reach the stack limit.
Other people have discussed the first assumption, but look at the second assumption: what if my video is only a fraction of a second long?
And sure, maybe that's not a very common use case. But are you really sure that nobody will upload a very short video? You're assuming that videos are a minimum duration, and you probably didn't even realize you were assuming anything! Could this assumption cause any other bugs in other places in your application?
Unidentified assumptions are a huge source of bugs.
Like I said, I know that your example is just an example, but this process of identifying your assumptions (which is often harder than it sounds) and then thinking of exceptions to those assumptions is a huge factor in deciding where to spend your time.
So if you find yourself thinking "I shouldn't have to program around this, since it will never happen" then you should take some time to really examine that assumption. You'll often think of corner cases that might be more common than you originally thought.
That being said, there is a point where this becomes an exercise in futility. You probably don't care if your JavaScript application works perfectly on a TI-89 calculator, so spending any amount of time on that is just wasted.
The other answers have already covered this, but coming up with that line between "this is important" and "this is a waste of time" is not an exact science, and it depends on a lot of factors that can be completely different from one person or company to another.
But a huge part of that process is first identifying your assumptions and then trying to recognize exceptions to those assumptions.