Rectangle 27 0

ffmpeg: Combinemerge multiple mp4 videos not working, output only contains the first video?


# this is a comment
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
ffmpeg -f concat -i mylist.txt -c copy output

I have used the example from the same reference page, it works pretty well. The only issue comes when video is not properly captured. It might introduce audio video sync issues because it just copies the PTS and DTS information from the source videos to the destination video. To get a full proof solution you need to read video packet-by-packet and then put together all the packets in one video with customized optimizations like dropping repeating PTS packets and maintaining monotonic PTS values.

Simple concat works for mp4 and mkv files if all the input videos have the same codec and you want output video also in same codec. https://trac.ffmpeg.org/wiki/Concatenate you need to creat a text file like

Note
Rectangle 27 0

ffmpeg: Combinemerge multiple mp4 videos not working, output only contains the first video?


cmd="( "

h264options="-vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 \
 -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
 -me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
 -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
   -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
 -qmax 51 -qdiff 4"

outfile="out-`date +%F-%H%M.%S`.mp4"

for i; do
    cmd="${cmd}ffmpeg -i $i -ab 256000 -vb 10000000 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 100 -f mpeg -; "
done
cmd="${cmd} ) | ffmpeg -y -i - -threads 8 ${h264options} -vb 10000000 -acodec libfaac -ar 44100 -ab 128k -s 1280x720 ${outfile}"
echo "${cmd}"
eval ${cmd}

It also sizes the videos to 720p but you may not want that.

MP4 files do not need to be converted into an MPEG format before being concatenated. In order to do the most simple concatenation, where all the input files are mp4 and the output file is also mp4, no transcoding is needed at all. The ffmpeg command line in Windows 7 (using bcmpinc's inputs.txt file, see below) is: ffmpeg -f concat -i inputs.txt -c copy output.mp4

The "h264options" are flags I recently found on the internet at h264.code-shop.com

This has the negative effect of transcoding the video twice. I can imagine you might have to transcode once but twice shouldn't be necessary.

You have to convert them into an MPEG format that can be easily concatenated. Below is a script I use and call "ffcat" for my GoPro videos. It essentially runs several "ffmpeg -i" commands which produce concatenate-able MPEG, which is piped to an ffmpeg command that then converts them to an H.264 mp4 file.

Note
Rectangle 27 0

ffmpeg: Combinemerge multiple mp4 videos not working, output only contains the first video?


ffmpeg -f concat -i inputs.txt -vcodec copy -acodec copy Mux1.mp4
file 75_540_38HQ2.mp4 
file 76_70_20.mp4 
file 76_173_80.mp4
file 81_186_35.mp4

Assuming you want to concatenate the movie, you can use the following command:

Note: some distributions (like Ubuntu) do not have ffmpeg in their repository and instead define ffmpeg to be an alias of avconv. This won't work with avconv, so in such a case you have to compile ffmpeg yourself. You can check whether you have the real ffmpeg by running ffmpeg and checking if the first output line ends with "the FFmpeg developers".

With the following text in inputs.txt:

Note
Rectangle 27 0

ffmpeg: Combinemerge multiple mp4 videos not working, output only contains the first video?


mp4box -add video1.mp4 -cat video2.mp4 -cat video3.mp4 output.mp4
mp4box

Forget about FFmpeg, use MP4Box instead, it is easy and faster:

If you are on Windows you can use YAMB which is a GUI for MP4Box that works great: http://www.videohelp.com/tools/YAMB

Some time ago FFmpeg has added a filter that can concatenate identical media files, here is the info: stackoverflow.com/a/11175851/218418

Note
Rectangle 27 0

ffmpeg: Combinemerge multiple mp4 videos not working, output only contains the first video?


# this is a comment
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
ffmpeg -f concat -i mylist.txt -c copy output
for f in ./*.wav; do echo "file '$f'" >> mylist.txt; done
printf "file '%s'\n" ./*.wav > mylist.txt

get the newest static binary from here:

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

It is possible to generate this list file with a bash for loop, or using printf. Either one of the following would generate a list file containing every *.wav in the working directory:

Note that these can be either relative or absolute paths. Then you can encode your files with:

Note
Rectangle 27 0

ffmpeg: Combinemerge multiple mp4 videos not working, output only contains the first video?


#!/bin/bash

################################################################################
#
# Script name: MultiMedia Concat Script (mmcat)
# Author: burek (burek021@gmail.com)
# License: GNU/GPL, see http://www.gnu.org/copyleft/gpl.html
# Date: 2012-07-14
#
# This script concatenates (joins, merges) several audio/video inputs into one
# final output (just like as if all the inputs were played in a playlist, one
# after another).
#
# All input files must have at least one audio and at least one video stream.
# If not, you can easily add audio silence, using FFmpeg. Just search the
# internet for "ffmpeg add silence".
#
# The script makes use of FFmpeg tool (www.ffmpeg.org) and is free for use under
# the GPL license. The inspiration for this script came from this FAQ item:
# http://ffmpeg.org/faq.html#How-can-I-join-video-files_003f
#
# If you find any bugs, please send me an e-mail so I can fix it.
#
################################################################################
#
# General syntax: mmcat <input1> <input2> <input3> ... <output>
#
# For example: mmcat file1.flv file2.flv output.flv
# would create "output.flv" out of "file1.flv" and "file2.flv".
#
################################################################################

# change this to what you need !!!
EXTRA_OPTIONS='-vcodec libx264 -crf 23 -preset medium -acodec aac -strict experimental -ac 2 -ar 44100 -ab 128k'

################################################################################
#
# NO NEED TO TOUCH ANYTHING AFTER THIS LINE!
#
################################################################################

# the version of the script
VERSION=1.3

# location of temp folder
TMP=/tmp

################################################################################

echo "MultiMedia Concat Script v$VERSION (mmcat) - A script to concatenate multiple multimedia files."
echo "Based on FFmpeg - www.ffmpeg.org"
echo "Don't forget to edit this script and change EXTRA_OPTIONS"
echo ""

################################################################################
# syntax check (has to have at least 3 params: infile1, infile2, outfile
################################################################################
if [ -z $3 ]; then
    echo "Syntax: $0 <input1> <input2> <input3> ... <output>"
    exit 1
fi

################################################################################
# get all the command line parameters, except for the last one, which is output
################################################################################
# $first  - first parameter
# $last   - last parameter (output file)
# $inputs - all the inputs, except the first input, because 1st input is
#           handled separately
################################################################################
first=${@:1:1}
last=${@:$#:1}
len=$(($#-2))
inputs=${@:2:$len}

# remove all previous tmp fifos (if exist)
rm -f $TMP/mcs_*

################################################################################
# decode first input differently, because the video header does not have to be
# kept for each video input, only the header from the first video is needed
################################################################################
mkfifo $TMP/mcs_a1 $TMP/mcs_v1

ffmpeg -y -i $first -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a1 2>/dev/null </dev/null &
ffmpeg -y -i $first -an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_v1 2>/dev/null </dev/null &

# if you need to log the output of decoding processes (usually not necessary)
# then replace the "2>/dev/null" in 2 lines above with your log file names, like this:
#ffmpeg -y -i $first -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a1 2>$TMP/log.a.1 </dev/null &
#ffmpeg -y -i $first -an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_v1 2>$TMP/log.v.1 </dev/null &

################################################################################
# decode all the other inputs, remove first line of video (header) with tail
# $all_a and $all_v are lists of all a/v fifos, to be used by "cat" later on
################################################################################
all_a=$TMP/mcs_a1
all_v=$TMP/mcs_v1
i=2
for f in $inputs
do
    mkfifo $TMP/mcs_a$i $TMP/mcs_v$i

    ffmpeg -y -i $f -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a$i 2>/dev/null </dev/null &
    { ffmpeg -y -i $f -an -f yuv4mpegpipe -vcodec rawvideo - 2>/dev/null </dev/null | tail -n +2 > $TMP/mcs_v$i ; } &

    # if you need to log the output of decoding processes (usually not necessary)
    # then replace the "2>/dev/null" in 2 lines above with your log file names, like this:
    #ffmpeg -y -i $f -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a$i 2>$TMP/log.a.$i </dev/null &
    #{ ffmpeg -y -i $f -an -f yuv4mpegpipe -vcodec rawvideo - 2>$TMP/log.v.$i </dev/null | tail -n +2 > $TMP/mcs_v$i ; } &

    all_a="$all_a $TMP/mcs_a$i"
    all_v="$all_v $TMP/mcs_v$i"
    let i++
done

################################################################################
# concatenate all raw audio/video inputs into one audio/video
################################################################################
mkfifo $TMP/mcs_a_all
mkfifo $TMP/mcs_v_all
cat $all_a > $TMP/mcs_a_all &
cat $all_v > $TMP/mcs_v_all &

################################################################################
# finally, encode the raw concatenated audio/video into something useful
################################################################################
ffmpeg -f u16le -acodec pcm_s16le -ac 2 -ar 44100 -i $TMP/mcs_a_all \
       -f yuv4mpegpipe -vcodec rawvideo -i $TMP/mcs_v_all \
    $EXTRA_OPTIONS \
    $last

################################################################################
# remove all fifos
################################################################################
rm -f $TMP/mcs_*

I have been using this script quite successfully. The results are perfect because it is using raw video.

Make sure you edit the EXTRA OPTIONS string.

Note
Rectangle 27 0

ffmpeg: Combinemerge multiple mp4 videos not working, output only contains the first video?


ffmpeg -i test1.avi -i test2.avi -vcodec copy -acodec copy \ -vcodec copy -acodec copy test12.avi -newvideo -newaudio

-newvideo, -newaudio, -newsubtitle have been removed from the current version. You have to use the -map option. (which I haven't wrapped my brain around.) If somebody wants to show how to use -map to string together MP4s that would be awesome.

From the ffmpeg man page "Examples" section:

I'm not sure if this question/answer belongs on SuperUser.

In addition to the first video and audio streams, the resulting output file test12.avi will contain the second video and the second audio stream found in the input streams list.

Note I'm having videos get corrupted if I concat very long files, so I'm going to try to mp4->mpeg then use simple concat.

The "-newvideo", "-newaudio" and "-newsubtitle" options have to be specified immediately after the name of the output file to which you want to add them.

The concat option in ffmpeg does not require the use of the -map option if you are really just concatenating the input files (e.g. you have two mp4 files, each 2 minutes in duration, and want to create a single mp4 file of 4 minutes duration as the output). The ffmpeg command line in Windows 7 (using bcmpinc's "inputs.txt" file, see above) is: ffmpeg -f concat -i inputs.txt -c copy output.mp4

These docs describe the concat filter and how to use the -map option in that case: ffmpeg.org/trac/ffmpeg/wiki/

You can put many streams of the same type in the output:

Note