KalyanChakravarthy.net

Thoughts, stories and ideas.

FFMPEG: Combine videos and re-encode

Over the past few weeks, I've taken to recording my Squash games using my Gopro to understand areas of improvement. The video is recorded at 1080p and 60fps, however Gopro splits the video into several large sized mp4 files.

I wanted to combine them together into a single file and re-encoded to reduce the file size significantly and ffmpeg came to rescue.

Combine Files

First order is to combine the files. ffmpeg accepts input text file with each line consisting of the object type ( file  in this case) and the file name. Alternatively it also accepts http and a url as a line item as well.

$ ls *.MP4 | perl -ne 'print "file $_"' > input.txt
$ cat input.txt 
file GOPR4466.MP4
file GP014466.MP4

Encode

To reduce file size one can do several things. The easiest is to set the crf flag which indicates "Constant Rate Factor" or the quality of encoding with values ranging from 0 to 51.  0 indicates lossless encoding and 51 the opposite. The default value is 23

$ ffmpeg -f concat -i input.txt -c:v libx264 -crf 28 output.txt

One-liner

$ ls *.MP4 | perl -ne 'print "file $_"' | ffmpeg -safe 0 -protocol_whitelist "file,pipe" -crf 28 -f concat -i - output.mp4