Using FFMPEG to Process downloaded USENET Movies

Last Updated 8/07/2020
ffmpeg [-r 30] -i a.mkv [-map 0:0 -map 0:1] -sn [-b:v 6.5M -r 30 || -crf 5] [-channel_layout '5.1' | -ac 2] [-metadata [...]] \
-ss: nn:nn:nn -t: nn:nn:nn [-target ntsc-dvd | -vf scale=640:480] [-max_muxing_queue_size n] [filters] [-c copy] -c:v libx[264|265] b.mp4
[-r 30] BEFORE the -i parameter, sets the input frame rate. AFTER the -i parameter, sets the output frame rate. Input frame rate may need to be used on some DVD-class files, especially if they are .AVI format, to prevent 'Duration too large' errors.
-i the input filespec. This can be a literal string or a batch parameter.
-map This allows you to remap the order of the streams present in the input file. Numbering of the streams starts at 0. For example, suppose the English audio is stream #1 but you want the German audio to be the default which is stream #2. You could use: -map 1:2 which means: the input stream #1 will be the current stream #2. Then don't map the current #1 to a different index and ffmpeg will thrown it out. Or you could keep it: -map 2:1. Using both map parameters in succession effectively switches the order of the audio streams.
-sn This removes all subtitling except hard coded subtitles (subtitles in the video stream itself).
-b:v n Set a constant bitrate for the video stream. Use constant bitrate when file size is more important than video quality and you don't want the risk of audio desyncing. These parameters are mutually exclusive
-crf nn

Set a variable bitrate (Constant Rate Factor) preset for the video stream. Use variable bitrate when video quality is more important than file size and you're willing to risk audio desyncing. For an explanation of this see below. For the x264 8-bit color CODEC (ffmpeg's default), The range of nn is 0–51, where 0 is lossless, 23 is the default, and 51 is worst quality possible. Same for the x265 10-bit color CODEC, except the default is 28. The ffmpeg team recommends values in the range of 17–28 for x264, and  22-33 for x265.

See CRF Explained for more info on this.

-channel_layout '5.1'

Forces the correct surround sound channel layout for the audio. As of 6-15-2018, there is a bug in FFMPEG that doesn't detect the channel layout in the surround sound audio correctly. If the input file appears to use a surround sound audio stream, and the actor's dialog is missing in the output file, this parameter usually fixes it.

True DTS surround sound was introduced in 1993, so any download claiming to have it for a movie that was shot before 1993 has faked it somehow. The first DTS movie was 1993's Jurassic Park.

The complete list of layout presets supported by FFMPEG are located here.

-ac n Sets the audio channels to n. For example, setting n to 2 will force stereo audio.
-ss: nn:nn:nn Starts encoding at the specified time in the video stream. Use these to create test and other clips, e.g. to test for cinavia or to test on playback hardware.
-t: nn:nn:nn Encodes for the specified amount of time.
-target ntscd-dvd The target parameter sets an easy to use conversion preset. In this example, the input file could be a PAL, and it will convert it correctly to NTSC.
-vf scale=nnnn:nnnn Sets the output video resolution.
-c copy copy the streams only; do not re-encode. In conjunction with VLC, this can work with older, stubborn DVDs where the audio refuses to sync with the video.
-pix_fmt yuv420p10le Use this to force 10-bit to 10-bit conversion if you are having problems with an x265 10-bit video. if you get this error: Incompatible pixel format 'yuv420p10le' for codec 'libx265', you will need to upgrade your version of the x265 lib to include 10-bit support.
-c:v libx264 HEVC/265 files (x265) are highly compressed and require a lot of processing power to decode, so a lot of older blu-ray players can't decode them. If your player cannot play 10-bit x265 files, you can use this to convert them backwards to 8-bit x264, ensuring that it's set to 10-bit to prevent dithering.
-c:v libx265 If your player can play 10-bit x265 files, you should use this to prevent striping and washed out colors. The resulting MP4 file may be larger and processing will be slower as ffmpeg has to process more data.
-async Audio sync method. "Stretches/squeezes" the audio stream to match the timestamps, the parameter is the maximum samples per second by which the audio is changed. -async 1 is a special case where only the start of the audio stream is corrected without any later correction. This option has been deprecated. Use the aresample audio filter instead.
[filters] Basic filter syntax is described in the filter guide. The filter documentation is here.
-max_muxing_queue_size n setting n to something huge fixes the "Too many packets buffered for output stream" bug.
-metadata Sets metadata for the file. Common entries are Title and Year, but you can use your own keys and multiple keys as long as each key is its own parameter. For example:
title="Ladder 49 [HD2K] (2004)" -metadata year="2004" -metadata class="HD2K"

Recommended bit and frame rate settings for FFMPEG based on class and resolution

Class Introduced Max Horiz Pixels Max Vertical Pixels Recommended Output Lib Recommended Constant bitrate (Optional)
Recommended Frame Rate
UHD4k 2016 3840 2160p x265 (10-bit) 12 - 24 Mbps Minimum 25 up to 60
UHD 2014 - 2016 2560 2160p x264 (8-bit) or x265 (10-bit), depending on how it was encoded 11 Mbps
"2k" 2009 - 2013 2048 1080p x264 (8-bit) 6 - 6.5 Mbps
HD2k 1920 1080p x264 (8-bit) 5.2 Mbps 25 or 30 only
All digital video classes 2010 or later may contain the Cinavia watermark
HD 2002 - 2008 1280 720p x264 (8-bit) 2.2 Mbps 25 - 30
DVD (SD) 1995 - 2001 720 480p x264 (8-bit) 796 Kbps 23 - 25
VHS (LD) 1972 - 1995 360 270p x264 (8-bit) 446 Kbps Maximum 23.796
DTS technology was introduced in 1993. The first film to feature DTS audio was Jurassic Park (1993). Any movie that was released before 1993 and has DTS audio means that the audio was re-encoded ("Fake DTS").
Film < 1972   varies x264 (8-bit) (use variable bitrate instead)*
* "the young teacher", a south korean indy, was the first movie to be released on VHS videotape. So, any movies that were released before 1971 may have been first transcribed from film to VHS, then from VHS to DVD, unless they were professionally transferred and digitally restored, such as The Wizard Of Oz. Movies in this class may need advanced parameters. It is recommended not to use constant bitrate in order to improve quality.

Common Display Resolutions Chart

Quality curves based on bit rate for the different HD Classes

Constant Bitrate (CBR) Vs. Variable Bitrate (VBR)

Exactly as it sounds, a constant bitrate spits out data at the same rate all throughout the video, and in streaming this means that viewers will receive data at the same rate. The problem with constant bitrates is that, unless the stream is just a static image, video segments will change. Some segments will have more detail and some have less detail. This translates to lower quality during complex segments and unused bandwidth on simple segments. Some situations will still use constant bitrates, but these instances are rare.

Variable Bitrate (VBR)

Variable bitrates are the data transfer process of choice for the majority of circumstances, because encoders will adapt to the detail demand of the segment. It essentially allocates more data during complex segments, and less data during simple segments. For instance, if your stream has a segment of you standing in front of a plain white background, the encoder will determine that the video needs less data and transfer less. If you streamed at a concert, the encoder will see varying details and transfer more data when needed.

If size of the final output is less important than its quality, use variable bitrate.

Be aware that the numbers and parameters for setting VBR are not standard and vary from codec to codec. Read the documentation for the CODEC you plan to use.

The best VBR encoders as of this writing are the x264 implementation (for 8-bit video), and the x265 implementation (for 10-bit video), both of which are included in FFmpeg.

Audio Desync

Some older DVDs, especially from the 90s, don't honor the correct signaling info when you convert to MP4. Blurays don't have this problem. Audio desync can also happen when you're trying to remove unwanted audio streams using the -map  parameter. When in Handbrake you check the "Align A/V Start" checkbox, Handbrake will set certain FFMPEG parameters to force the physical alignment of the audio & video streams. How Handbrake does this is still under investigation. Unfortunately as of this writing, even with the use of Handbrake some stubborn DVDs still cannot be reencoded without audio desync; the audio and video streams for these have to be copied to a new container. VLC using the File | Convert Stream... option often works.

Decrypting Downloaded DVD Images

If what you've downloaded off the USENET is not a video file but a copy protected VIDEO_TS type DVD folder, you can use makemkv to convert the folder into an MKV file.


Video File Concatenation Explained

FFmpeg has three concatenation methods.

1. concat video filter

$ ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \   
         -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" \   
         -map "[v]" -map "[a]" output.mkv

Note that this method performs a re-encode. Recommended when your input files have differing properties.

2. concat demuxer

$ for i in *.mp4; do printf "file '$i'\n" >>a.txt; done && ffmpeg -f concat -i a.txt -c copy out.mp4 && rm 'a.txt'

This is a one-line, very fast way to concatenate multiple files without re-encoding.

3. concat protocol

$ ffmpeg -i "concat:input1|input2" -codec copy output

This method does not work for many formats, including MP4, due to the nature of these formats and the simplistic concatenation performed by this method.

Which one to use

  • concat filter: Use if your inputs do not have the same parameters (width, height, etc), or are not the same formats/codecs, or if you want to perform any filtering. (You could re-encode just the inputs that don't match so they share the same codec and other parameters, then use the concat demuxer to avoid re-encoding the other inputs).
  • concat demuxer: Use when you want to avoid a re-encode and your format does not support file level concatenation (most files used by general users do not support file level concatenation).
  • concat protocol: Use with formats that support file level concatenation (MPEG-1, MPEG-2 PS, DV). Do not use with MP4.

If in doubt try the concat demuxer.

Latest Matroska Guides: