Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMPEG compressed MP4 video not playing on Mozilla Firefox with a "file is corrupt" error

Tags:

php

ffmpeg

I have compressed MP4 video using FFmpeg in a PHP environment. Videos are being compressed, but they are not playing in Firefox, showing an error:

Video can't be played because the file is corrupt

while this video is playing fine in VLC media player and also in the Chrome browser. My code of compression is as-

exec("ffmpeg -i input.mp4 -acodec mp2 output.mp4");
like image 810
user2519007 Avatar asked Dec 23 '13 11:12

user2519007


2 Answers

I struggled with this problem until I discovered this gist entitled 'ffmpeg convert gif to mp4, for best cross browser compatibility'. It uses this command:

ffmpeg -f gif -i FOO.gif -pix_fmt yuv420p -c:v libx264 -movflags +faststart -filter:v crop='floor(in_w/2)*2:floor(in_h/2)*2' BAR.mp4

It has these notes on how it works:

  • output mp4 is encoded with h264, support Firefox/Chrome/Safari in Windows, Mac OSX, Android, and iOS.
  • one mp4 file for all platforms, there is no need to encode an extra webm movie, which encoding speed is pretty slow.
  • format as yuv420p for Firefox compatibility, the downside is color becomes less-saturate than original gif.
  • yuv420p only support even width/height, so crop filter is required
  • -movflags +faststart flags are optimized for online view in browser
  • compression ratio typically 10:1, pretty awesome. note that if original gif is < 512KB, convert as mp4 is less efficient.

Incorporating that into my ffmpeg command, I find that the videos now run in Firefox, Safari, Opera and QuickTime (where previously only Chrome and VLC worked for me).

Credit to ingramchen, who wrote the gist.

like image 83
Michael Keenan Avatar answered Oct 16 '22 08:10

Michael Keenan


I know this is a few months old, but in case anyone is still intereste: I had this same thing happen and I found it was because my MP4s were encoded in "MPEG-4 Simple profile" a.k.a. H.263/MPEG4 part 2/xvid. These formats are not supported for playback in most browsers any more.

Transcoding the files to H.264 fixed the problem.

like image 20
Jon Avatar answered Oct 16 '22 10:10

Jon