Using FFMPEG to join two MTS files together

15,498

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
15,498
Reado
Author by

Reado

Updated on June 05, 2022

Comments

  • Reado
    Reado almost 2 years

    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.

  • Kouber Saparev
    Kouber Saparev about 12 years
    I get this error, when I try the first command above: "File for preset 'hq' not found"
  • Reado
    Reado about 12 years
    Look at the ffmpeg documentation. "hq" was replaced a while ago.
  • sir_brickalot
    sir_brickalot over 8 years
    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.
  • dmgig
    dmgig almost 8 years
    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).
  • TobiX
    TobiX almost 8 years
    Your first step does reencode the files and looses quality.
  • Sriharsha Madala
    Sriharsha Madala over 2 years
    In case there are a lot of files : ffmpeg -i "concat:$(cat Filenames)" output.mp4