Using ffmpeg to convert from MTS to mp4
I have a bunch of memory cards packed with MTS files, that I recorded in few years. Now I would like to convert them in mp4, in order to watch them on my TV or iPad. I used the awesome ffpmeg to concatenate and convert them.
1. Create a text file with filenames
First I created a text file with the names of all origin videos. Every line in the file is like this:
file 'subdirectory/filename.MTS'
I just used the ls
command (ls AVCHD/BDMV/STREAM/*MTS > files.txt
) and few substitutions in a text editor to create it.
2. Conatenate
The fastest command is just a concatenation:
ffmpeg -f concat -i files.txt -c copy movie.mts
But you may want to create video files for DVDs, or save space using an H.264 encoding. For DVDs a command could be:
ffmpeg -f concat -i files.txt -target pal-dvd movie.mpg
For an mp4 file:
ffmpeg -f concat -i files.txt -c:a copy -c:v libx264 -preset slow -crf 18 -f mp4 movie.mp4
You may tune the -preset
and -crf
parameters to have better quality, or faster encoding.
3. Do not disturb, please
Converting a video is a tough job: everything will slow down while doing. So, if you use your computer at the same time, you may want to put the conversion on a lower priority, using the command renice
.
First you need to know the PID of the ffmpeg process with:
ps –al
Then you change its priority with renice:
renice -n 19 PID
Where ‘19’ is the lowest priority: it will takes a little more, but it will doesn’t bug you too much.
For example:
ps –al
...
renice –n 19 654
4. What about an iPad or Android tablet?
To optimize your videos for iPads, ffmpeg gives you some more flags:
ffmpeg -f concat -i files.txt -vcodec libx264 -profile:v main -level 3.1 -preset medium -tune film -x264-params ref=4 -acodec aac -ac 2 -strict experimental -movflags +faststart -f mp4 movie.mp4
It looks like that family videos may benefice of -tune film
parameter, while the -level
depends on your iPad model. You may also need to scale down your video with: -vf "scale=-2:720:force_original_aspect_ratio=decrease"
.
More information in ffmpeg.org web site, or on Wikipedia.