14

What list of commands should I do in ffmpeg/mencoder/some_other_CLI_app to get a video in all the right formats to use Video for Everybody?

i.e. Is there a subtlety that prevents there being a mechanical tutorial that says

VIDEO=video.mp4
TYPE=$(ffmpeg --find-information-about-the-video ... )
ffmpeg ... > video.mov
ffmpeg ... > video.flv
ffmpeg ... > video.webm
ffmpeg ... > video.h.264
...

Then copy and paste the Video for Everybody code into your page?

My goal is to have an embedded video in my personal webpage hosted by me. HTML5 with flash fallback is preferred, so the simplest way seems to be Video for Everybody. But that page leaves the most complicated part, encoding the video into all the different formats, to the reader to figure out by reading a long and complete article about video formats. Surely this isn't necessary. I could provide a list of commands for a user to blindly convert an audio file into a bunch of different formats. Hopefully an ffmpeg guru could do the same for video.

1
  • 1
    Minor comment to your pseudocode examples: There's no .mov for HTML5 video, and there's no h.264 extension either. There is .264, which is the raw Annex B bytestream for NAL units. Video encoding is no rocket science, but you need to invest a little time to get to know the concepts.
    – slhck
    May 13, 2012 at 16:12

1 Answer 1

21

Note: This answer is probably not what you need, and it has been heavily edited since its original posting in 2012. It's 2022 now; streaming works differently than it used to. This guide assumes simple progressive download of one video file at one given resolution, e.g. 1080p – no adaptive streaming. If you need adaptive streaming (MPEG-DASH or HLS), please search for other guides.

Requirements

First off, make sure to download a recent ffmpeg version (download a static build; don't call apt-get install ffmpeg or similar).

To generate videos supported by the most browsers, always check the latest compatibility table. The most common supported video codecs and containers are:

  • H.264 or H.265 video in an MP4 container
  • VP9 or AV1 video in a WebM/MKV container

As H.264 and VP9 are faster to encode, this answer will only focus on them. To use the other codecs (H.265, AV1), check the FFmpeg Wiki Encoding Guides.

WebM (VP9 / Vorbis)

Follow the recommendations in the FFmpeg VP9 guide and use a two-pass encoding approach with rate constraints:

ffmpeg -y -i input-c:v libvpx-vp9 -b:v 2000k -minrate 500k -maxrate 2500k -c:a libopus -pass 1 -f webm /dev/null && \
ffmpeg -i input-c:v libvpx-vp9 -b:v 2000k -minrate 500k -maxrate 2500k -c:a libopus -pass 2 output.webm

The target bitrate depends on your resolution, frame rate, the type of content, and what quality you want. 2.5 MBit/s should be a good compromise for HD video at 30 fps. See this Google guide for some recommendations.

MP4 (H.264 / AAC)

Follow the recommendations in the FFmpeg H.264 guide and use a two-pass encoding approach with rate constraints. The 2-pass bitrate approach is preferred for streaming-type applications:

ffmpeg -y -i input -c:v libx264 -b:v 5000k -minrate 1000k -maxrate 8000k -pass 1 -an -f mp4 /dev/null && \
ffmpeg -i input -c:v libx264 -b:v 5000k -minrate 1000k -maxrate 8000k -pass 2 -c:a aac -b:a 192k -movflags faststart output.mp4

Here, the target bitrate should be about 50% higher than for VP9 video, as H.264 is not that efficient. Add the -movflags faststart option to the second pass to make initial loading of the video faster.

For setting audio options, see the AAC encoding guide.

6
  • 1
    The 2013-12-19 version of one of your paragraphs cuts off in mid-sentence. "The audio defaults to 128 kBit/s again, but you can set q:a for variable bitrate, where values range from 0 to 500. 100 is default and higher means better. If" May 2, 2014 at 18:57
  • @slhck makes a lot of sense, but my two cents are that you drop Ogg and replace it with mpegts/m3u8 to support all the iOS devices or those that accept HLS. You can reuse the .mp4 files and just add the mpeg-ts processing in ffmpeg. Dec 20, 2014 at 15:41
  • @Mondain Indeed, but MPEG-TS/.m3u8 is not generally HTML5-compatible video. You need an HLS server and you need to know a little more about streaming, which is not really within the scope of this question.
    – slhck
    Dec 20, 2014 at 15:49
  • Good points, but from my experience thus far a simple http server can serve m3u8 to a video tag. The only thing one normally needs to do is add the mime types for m3u8 and ts to the respective server configuration. Dec 20, 2014 at 15:52
  • How's this in 2022?
    – Kodeeo
    Oct 13 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.