Using FFMPEG to join two MTS files together
Asked Answered
U

5

12

I have two MTS video files, each one 2 minutes long. I need to be able to join the files together and convert the format to MPEG4. I have a suitable command line for converting MTS to MP4 but don't know how to join the files together in the first place.

Some articles on the web suggest using the CAT command, like:

cat video1.mts video2.mts > whole_video.mts

However this doesn't work and according to FFMPEG, "whole_video.mts" is only 2 minutes long, not 4 minutes.

Does anyone know how to join the files together? Is FFMPEG the best program to use to do this? Thanks in advance.

Utterance answered 24/8, 2010 at 8:43 Comment(1)
On MacOS the command works well. This is a great help to handle the files MTS.Indisputable
T
24

The following worked perfectly for me (i.e. resulting in seamless joins):

ffmpeg -i "concat:00019.MTS|00020.MTS|00021.MTS|00022.MTS" output.mp4
Tynan answered 5/8, 2015 at 15:55 Comment(4)
Just adding this as a newbie: I had to cd into the source folder and run command from there (I even copied the ffmpeg bineries in the folder but thats probably not necessary). First I tried to cd in to ffmpeg folder run command and use absolute paths for the files to concatenate. That didn't work.Excurved
Worked well for me. Is there a way to accomplish this without re-encoding? I tried "output.MTS" but it still seemed to want to go through frame by frame (it was faster though).Jevons
In case there are a lot of files : ffmpeg -i "concat:$(cat Filenames)" output.mp4Idealist
@SriharshaMadala your command results in the following error cat: Filenames: No such file or directoryRight
E
3

Using cat works. Its just that video players will be kind of fooled about the video length while reading the resulting whole_video.mts. There will be typically a sudden timestamp jump where the file were previously cut. But this is okay. You can encode it and then you'll get a right timestamped file.

Encoding with ffmpeg and then joining with MP4Box is a bad idea. You'll get ugly images with missing blocks at the crossing position if the second file doesn't start with a keyframe (which happens when it has been cut by a camcorder because of the 2GB file limitation). Do join and then encode, not the opposite.

Exclusion answered 29/1, 2012 at 12:5 Comment(0)
U
1

It's OK, I've sorted it. Using the latest SVN versions of FFMPEG, x264 and MP4Box (GPAC), here's what I did...

Use FFMPEG to convert the MTS files to MP4 as normal:

ffmpeg -i video1.mts -vcodec libx264 -deinterlace -crf 25 -vpre hq -f mp4 -s hd480 -ab 128k -threads 0 -y 1.mp4
ffmpeg -i video2.mts -vcodec libx264 -deinterlace -crf 25 -vpre hq -f mp4 -s hd480 -ab 128k -threads 0 -y 2.mp4

Use MP4Box to join the MP4 files together:

MP4Box -cat 1.mp4 -cat 2.mp4 output.mp4

This joins the files together into "output.mp4", however when I use "ffmpeg -i output.mp4" it says the duration is longer that it should be. To fix this, I had to use FFMPEG again:

ffmpeg -i output.mp4 -vcodec copy -y final.mp4

And voila! Querying the "final.mp4" file using FFMPEG shows the correct duration and the video plays fine.

Hope this helps anyone else experiencing the same problem.

Utterance answered 24/8, 2010 at 10:42 Comment(3)
I get this error, when I try the first command above: "File for preset 'hq' not found"Bibliophage
Look at the ffmpeg documentation. "hq" was replaced a while ago.Utterance
Your first step does reencode the files and looses quality.Bibbye
P
1

To avoid re-encoding, -c copy is needed

ffmpeg -i "concat:00032.MTS|00033.MTS" -c copy out.MTS
Patsypatt answered 11/9, 2023 at 21:27 Comment(0)
L
1

Using the solutions listed here or anywhere else on the web didn't work well for me sadly - either the audio was missing from the output file or the encoding didn't happen properly which meant the file wouldn't play smoothly and/or was missing parts. I'm not sure if it was because it was a crappy AVCHED compiling issue or an outdated codec, but nothing worked.

I finally found a workflow of programs that worked for this .MTS file format.

Combining Videos The program MKVToolNix is the only thing that successfully combined the files without messing up the output file, or stutter & loss of audio, or re-encoding it.

ffmpeg, other programs that used ffmpeg, shutterencoder, & other tools failed for me.

Converting to MP4 Converting .MTS directly to MP4 resulted in a loss of Audio as well. I had to convert .MTS -> .MKV using MKVToolNix, and then convert the .MKV -> .MP4 using Handbrake

Using the combination of above tools one should be able to work with .mts files if anything else fails

Loferski answered 15/9, 2023 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.