Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaElement.js - Flash Video Wont Play Until Fully Loaded

I'm having an issue will MediaElement.js (my flash video player) not playing my .m4V video files (encoded using ffmpeg) until the video is fully loaded.

I have tested this with various third party video files that appear to play straight away before they have completely loaded. Only my files do not :(

When played using the html5 video solution they play straight away, just not on the flash fallback.

Could this be to do with the settings the video is being encoded at? I don't see any other reason.

CODE:

<video id="player1" src='BriefTour.m4v' type="video/mp4" preload="none"></video>

<script>
var videoPlayer = MediaElement('player1',{
  success:function(me){me.play();}              
});
</script>

As you can see I am specifying no option, the player is on default settings.

Hope someone has encoutered this before and can help me out!

like image 628
wilsonpage Avatar asked Feb 20 '11 18:02

wilsonpage


3 Answers

As you are encoding your own videos and using FFmpeg, I suggest using 'qt-faststart'.

This is a useful tool included with FFmpeg that rearranges a file with H.264 video, "such that the moov atom is in front of the data, thus facilitating network streaming". Basically, it allows web movies to start playing before they are completely downloaded.

Enable it with the following the ffmpeg directory:

cd ~/ffmpeg
make tools/qt-faststart

Usage (after your ffmpeg encoding):

qt-faststart input.foo output.foo

This should allow your player to play the video while it is still downloading.

like image 141
Kit Avatar answered Oct 20 '22 01:10

Kit


Flash can't always play MP4s if they are not indexed in the way it wants.

To fix your file, just download this: QTIndexSwapper

like image 31
John Dyer Avatar answered Oct 20 '22 01:10

John Dyer


I tried qt-faststart with my own MP4 (h264+AAC) encoded files and was always getting an "last atom in file was not a moov atom" error message (and no output file). I was then assuming my files were ok and searched for the problem elsewere. After hours of testing, my assumption proved to be wrong - indeed, it seems my files had no moov-atom at all, not at the beginning nor at the end!

To succesfully fix this I used ffmpeg first to "regenerate" the file - that is, remux the original h264+AAC tracks into a new MP4 file without reencoding it:

ffmpeg -i souce_file.mp4 -acodec copy -vcodec copy target_file_1.mp4

After this, the new file should have its proper moov-atom at the end. So now you can use qt-faststart in order to move it to the beginning, as Kit explained in his answer:

qt-faststart target_file_1.mp4 target_file_2.mp4

After doing that, mediaelement plays all my videos just right after clicking on the play button, when the file starts downloading! :)

If your problem is you already have all your files in a youtube-like site, your hosting is Linux-based, ffmpeg is not there and you cannot compile it on your own, you my find it useful getting a static build of ffmpeg. You can find that here:

http://ffmpeg.gusari.org/static/ (32 bit) or here: http://dl.dropbox.com/u/24633983/ffmpeg/index.html (64 bit)

Unfortunately, on the 32-bit build I used there was no qt-faststart, not as binary nor as source code. In this case you can download it from ffmpeg SVN and compile it directly with gcc. I did it succesfully in my ultra-el-cheapo shared hosting. It doesn't seem to have any build dependencies. Or you can even try my own qt-faststart binary build and see if it works for you.

EDIT: I've just discovered that in newer versions there's no need for qt-faststart at all. You can encode directly with ffmpeg using the following option:

-movflags +faststart 
like image 43
Pere Avatar answered Oct 20 '22 01:10

Pere