Miscellaneous Notes on using FFMPEG

FFMPEG is an inredibly powerful utility. You can do so many things with it that it can become hard to keep track of everything. So I started writing them down, and figured I'd add them here as sort of a backup.

Concatenating Two or More MP4 Files Losslessly

ffmpeg -f concat -i file1.mp4 file2.mp4 -c copy output.mp4

For more than two files, the following may work better: Create a text file named vidlist.txt (or filelist.txt or whatever filename you prefer) in the following format:

file '/path/to/clip1'
file '/path/to/clip2'
file '/path/to/clip3'

Note that these can be either relative or absolute paths.

Then issue the command:

ffmpeg -f concat -safe 0 -i vidlist.txt -c copy output.ext

Converting MPEG-2 to H.264 MKV

ffmpeg -i input.mpg output.mkv

There are probably ways to get a better output, but this worked just fine for my purposes.

ffmpeg -i input.mpg -c:v libx264 -preset slow -crf 19 -c:a copy output.mkv

This worked, allowing me to keep the original audio. FFMPEG gave me a warning about a library mismatch, so my version of FFMPEG probably isn't using libx264. But it still wrote an AVC format into the MKV container, so all was well. I may try it without a library specified at some point just to see what happens. The output was all right, but I think I would have preferred a higher bitrate. So I may specify a lower crf next time.

Cutting Part from Video File from Start Position to End Position

ffmpeg -ss [start] -i in.mp4 -t [duration] -c copy out.mp4

Here, the options mean the following:

-ss specifies the start time, e.g. 00:01:23.000 or 83 (in seconds)
-t specifies the duration of the clip (same format).
Recent ffmpeg also has a flag to supply the end time with -to.
-c copy copies the first video, audio, and subtitle bitstream from the input to the output file without re-encoding them. This won't harm the quality and make the command run within seconds.

Better method if the version used supports it:

ffmpeg -i b.mkv -c copy -ss 00:00:20 -to 00:50:04 b2.mkv

If starting from the beginning of the file, -ss can be omitted: ffmpeg -i 1.mkv -c copy -to 01:26:53 b2.mkv

Extracting Embedded Subtitles to an External Text File

I had a file that had embedded subtitles, but I couldn't get them to display. So I used this command:

ffmpeg -i Movie.mkv -map 0:s:0 subs.srt

And named the SRT file to the same name as the MKV file, and the subtitles displayed just fine.

Merging Audio File With Video File Losslessly, where Video File Has No Audio

ffmpeg -i videofile.ext -i audiofile.ext -c copy outputfile.ext

Example:

A video file and an audio file have been renamed to a.ext for convenience:

ffmpeg -i a.mp4 -i a.aac -c copy output.mkv

Removing Subtitles from Video Without Re-encoding

I was trying to remux an MP4 file into an MKV and got two errors. The first was

Subtitle codec 94213 is not supported

and the second was

Could not write header for output file #0 (incorrect codec parameters ?): Function not implemented

Turned out, after some experimenting, that ffmpeg just didn't like that subtitle file at all. So, I looked up on the internet how to remove it:

ffmpeg -i video.mkv -vcodec copy -acodec copy -sn video-no-subs.mkv

And I was able to remux just fine.

Remuxing MP4 to MKV

ffmpeg -i input.mp4 -codec copy output.mkv

Ripping from Decrypted DVD-R

First, I used cat file1 file2 etc. > file.vob to stitch the VTS_01_x.VOB files together.

Then, I used this line:

ffmpeg -i file.vob -vf "fieldmatch,yadif,decimate" -preset slow -crf 21 -c:a copy 1.mkv

The crf level would vary depending on the episode or movie. This one was an hour and a half, and 21 got it down to 1.29 gigs, which wasn't too bad. The yadif settings were for inverse telecine (video pattern of two frames interlaced, three not). For top frame first interlacing, I'm using a simple yadif setting that causes it to detect the interlacing and deal with it:

ffmpeg -i file.vob -vf yadif -preset slow -crf 19 -c:a copy 1.mkv

Adding -r 30000/1001 after the input file may prevent problems with audio sync in some circumstances, such as the Past duration...too large warning.