97

I want to merge videos in batch size of twenty (20) each. I'm running a Linux machine. The videos are in mp4 format and moderate quality. Some even have the audio stream missing. So far I've tried ffmpeg, mencoder, cvlc/vlc and MP4Box. I want to write a command line script to achieve this, since I'm doing batch processing.

The main issue is that some of the solutions I tried work well for two videos, some work well for videos with audio stream and yet others work well for some other subset of my video set. However, I have not been able to find a comprehensive solution for this task.

Dhruv Singal
  • 1,221

11 Answers11

90

I am using mkvmerge to join multiple MP4 files into single one:

mkvmerge -o outfile.mkv infile_01.mp4 + infile_02.mp4 + infile_03.mp4
Elder Geek
  • 36,752
57

You can do it using ffmpeg:

ffmpeg -i concat:"input1.mp4|input2.mp4" output.mp4

reference and more info

Maythux
  • 87,123
47

Create a file files.txt with all the files you want to have concatenated in the following form (lines starting with a # are ignored):

# this is a comment
file 'file1.mp4'
file '/path/to/file2.mp4'
file 'file3.mp4'

Note that these can be either relative or absolute paths. Then you can stream copy or re-encode your files:

ffmpeg -f concat -safe 0 -i files.txt -c copy output.mp4
ShaDeRzz
  • 571
15

This solved the matter: https://stackoverflow.com/a/22324018/5014767

melt is a great command line utility for this. Here is the page

Edit from comments: The command which solved my problem was this melt {input-sequence} -consumer avformat:{output-name} acodec=libmp3lame vcodec=libx264

karel
  • 122,292
  • 133
  • 301
  • 332
Dhruv Singal
  • 1,221
8

I wrote a little shell script to concat MP4s without transcoding using ffmpeg.

for f in $(ls *.MP4); do
    ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
done

CONCAT=$(echo $(ls *.ts) | sed -e "s/ /|/g")

ffmpeg -i "concat:$CONCAT" -c copy -bsf:a aac_adtstoasc output.mp4

rm *.ts

This creates intermediate files in an MPEG container and then concatenates them into an MP4.

RawwrBag
  • 179
4

I wrote a wrapper around ffmpeg called vidmerger , which makes it very easy to merge multiple videos inside a folder, for instance to merge all mp4 files inside the current directory, just run:

vidmerger .
2

LossLessCut works very well for this. Fast and painless.

0

I created a Python script that, using moviepy, can concatenate also subsegments (useful if you want for example remove some parts from a video).

Use it with:

vcat -i inputfile1,inputfile2[start-end],... -o <outputfile>
Antonello
  • 779
  • 1
  • 13
  • 26
0

I'd like to throw in my "videomerge" - available for ubuntu via ppa:

Its a QT5 GUI app using drag'n drop. To install:

sudo add-apt-repository ppa:jentiger-moratai/mediatools
sudo apt update
sudo apt install videomerge

As all of my apps, I try to keep the UI very simple:

1

kanehekili
  • 7,426
0

My script, purely in bash and ffmpeg. Re-encodes files given on a command line into one.

Requires the files to be of the same resolution. Accepts different metadata rotation, which ffmpeg concat: doesn't.

rm ffmpeg-concat-output.mkv

FILE_COUNT=$#
INPUTS=""
FILTER=""
INDEX=0
for FNAME in $@; do
    echo "Processing ${FNAME}"
    INPUTS="${INPUTS} -i $FNAME"

    if [ -z "${FILTER}" ]; then
        FILTER="[$INDEX:v:0] [$INDEX:a:0]"
    else
        FILTER="${FILTER} [$INDEX:v:0] [$INDEX:a:0]"
    fi
    let INDEX+=1
done

COMMAND="ffmpeg ${INPUTS} \
    -filter_complex '${FILTER} \
        concat=n=${INDEX}:v=1:a=1 [v] [a]' \
    -map '[v]' -map '[a]' \
    ffmpeg-concat-output.mkv"

#   -af 'volume=15dB' # won't work with -filter_complex

bash -c "${COMMAND}"
derHugo
  • 3,376
  • 5
  • 34
  • 52
-2

In addition to Maythux's answer, from :

A few multimedia containers (MPEG-1, MPEG-2 PS, DV) allow one to concatenate video by merely concatenating the files containing them.

I.e. the videos to be concatenated use one of the abovementioned video containers (to which I'll add MPEG-4 Part 14 for personal experience), you could simply:

cat video1.ext video2.ext video3.ext > video4.ext
Maythux
  • 87,123
kos
  • 41,268