“Today is another day of hope.”

background

Recently, I am making a demand for video playback. Because it is relatively simple and only needs to implement basic functions such as loading, playing and pausing, I use The Android player MediaPlayer. Originally happy to finish the work, is secretly to sister wechat, test sister with a phone came over a snap output

“Ah, why is your video loading so slow? !”

“If you look at iOS that doesn’t work, is that going to work? !”

Oh, my god, when I tested it clearly fine ah, how now it takes more than 30 seconds to load a video. Looking at the spinning loading diagram, I cursed

“There’s another fucking hole, isn’t there?”

In order not to delay your time, let’s jump to the conclusion: the video metadata is in the wrong location

????? What is video metadata? What’s the wrong location? You need someone else to baidu for technical writing, right?

No, no, no. Let me explain

A key sentence

The video format used in the actual project is mostly MP4, which is composed of boxes. This Box can be interpreted as a block of data. You can nest boxes inside a Box:

(If you want to try it yourself, you can use MediaParaser for MAC, Mp4Info for Window)

Ftyp, MOOv, AND MDAT are the names of Box. The two boxes that need to be focused on here are MOOV and MDAT.

A MOOV is the metadata of a video, which contains the overall information about the video, such as length, bit rate, width and height

Mdat is specific media data, which is what we play

This sentence is crucial

The player can only play the video when it gets the MOOv box!!

The player can only play the video when it gets the MOOv box!!

The player can only play the video when it gets the MOOv box!!

This sentence is the heart of the article. Don’t worry about the rest. At least remember that

So about the player loading video slow reason, smart brother must have already had the answer, continue to see nothing but to verify it

why

The picture above shows the ideal location of the video box.

But when some videos are suppressed, the MOOv box is placed at the end of the video, after the MDAT. This means that the player has to download the entire video before it can get to the Moov Box and play it.

It’s like when you go on a date with a girl, and your boss tells you that there is a problem online that he wants you to solve, and then you go on a date and see if your sister will break up with you on the spot

Plan a

The immediate reason is to find it. The wise brother would certainly ask another question

If the Moov Box is so critical, why do some videos put it last?

I did some experiments on this and found that ffMPEG will put the MOOv box at the end of the video by default when converting video formats. I guess ffmPEG has to convert the entire video to know exactly how long the new video will be, the bit rate, etc., so it’s easy to put it at the end of the new video.

Ffmpeg actually also provides a command to move the MOOv box to the video header

ffmpeg -i input.mp4 -c copy -f mp4 -movflags faststart output.mp4
Copy the code

Isn’t that one of the solutions?

Try it with the new video, and the loading time is indeed 2~3 seconds

The truth is out!!

Scheme 2

Just when I was refreshed and ready to communicate with my sister again, my product aunt told me that we have tens of thousands of videos and the project will be online soon. Who gives you time to convert one by one?

I’m just a part-time worker… Don’t scold me..

There are situations in development where you can’t make changes to the video source, and we have to figure it out on our own.

The premise of video playback is to get the MOOV box, and the video source MOOv box is at the end, so can we

Ask the end of the video to get the MOOv box, and then ask the video from the beginning?

B: Sure. The streaming media request is not completed in a single request, but in a fragmented request. Make an HTTP request, read the beginning of the response body, and move on to mdAT if you find mooV at the beginning. If not, the second request reads the data directly at the end of the file, so the MOOV can be retrieved with two requests. This solution requires the server to support request-range requests, i.e. to read the end of the file directly through the Range, but the general OSS services support this.

This one works! But do I have to rewrite the request for MediaPlayer?? What about my sister? !

On second thought, this problem should be very common, clever brothers must have filled the pit. So I tested it with ijkPlayer and exoPlayer, and sure enough, both players have already handled this situation, so just replace it. IjkPlayer’s official SO library did not support HTTPS requests, so exoPlayer was eventually adopted

Yeah! It’s finally over! Although my sister has long been impatient not to return my messages, BUT I believe that as long as I insist on being good to her, she will be touched. Next pit, I will not keep my sister waiting

I am the wood

Trying to make technology less boring

I want to use the technology in my hand to solve some problems, to achieve something

Want to make money, want to grow

Welcome you to exchange attention ~~

Reference article Alibaba engineer so seconds open short video