ffmpeghtml5-videovideo-encoding

How to process a video to mp4 with ffmpeg for quality and compatibility?


I am beginning to be more serious about video. I am processing my videos with ffmpeg in a fully updated Linux into mp4 to use it in HTML5 directly.

Now, I have old AVI videos that I want to convert to mp4 with ffmpeg for use with HTML5. In particular, I have this one:

http://luis.impa.br/photo/1101_aves_ce/caneleiro-de-chapeu-preto_femea_Quixada-CE-110126-E_05662+7a.avi

(I know, terrible quality... sorry). According to ffprobe:

Duration: 00:01:35.30, start: 0.000000, bitrate: 1284 kb/s
Stream #0:0: Video: mpeg4 (Simple Profile) (DX50 / 0x30355844), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 1144 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc
Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, stereo, s16p, 128 kb/s

That seems perfect: mpeg4 video and mp3 audio. So I tried:

ffmpeg -i input.avi -acodec copy -vcodec copy output.mp4

It generates a file that plays nicely in mplayer, but not in firefox getting an error:

Video format or MIME type not supported.

Chrome plays the audio, but no video is shown... Now, if I do:

ffmpeg -i input.avi output.mp4

firefox works, but the video is reencoded in another one with half the size (half the bitrate). This is what ffprobe says about the reencoded video:

major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf57.71.100
Duration: 00:01:35.30, start: 0.000000, bitrate: 685 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 548 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)

I suppose that I am loosing lots of quality (and time processing the video). So, my questions:

  1. Why are browsers not playing my video with the copy codecs ?

  2. Can I work with ffmpeg in this particular file without reencoding? If yes, how?

  3. If I need to reencode, which are "reasonable" parameters to keep close to the original quality? Would something like

    ffmpeg -i input.avi -b:v 1024k -bufsize 1024k output.mp4

suffice for this video? This generates a new video with size closer to the original one.

Thanks!


Solution

  • According to ffprobe and if I see it correctly, you have a DivX (5) video file. Do not use it for web!! ;)

    mpeg4 (Simple Profile) (DX50 / 0x30355844)
    

    So I don't see any chance to use this video without reencoding. Not if you want wide browser support.

    Use VP9/WebM or h264: https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats

    Some encoding tips:

    Settings that yield a good re-encode depends on your input (quality, resolution, fps, the type of content...), so there is no standard answer.

    But you have to specify a codec, or let ffmpeg choose one depending on your output file extension (which you should only do once you have a good intuition of what extensions get which codecs).

    You can try this:

    ffmpeg -i input.avi -c:v libx264 -preset slow -crf 22 -c:a copy output.mp4
    

    libx264 generally has decent defaults for encoding h264, but there's a plethora of settings if you feel like micromanaging it: https://trac.ffmpeg.org/wiki/Encode/H.264

    VP9 potentially can yield better quality-for-bitrate than h264, but its encoder is considerably slower by default.

    ffmpeg -i input.avi -c:v libvpx-vp9 -speed 5 -row-mt 1 -crf 30 output.webm