I have big videos with different types of properties and trying to collect some part of these videos by ffmpeg.

I use this command to cut specific parts:

scale video >> scale png >> pad scaled_video >> add icon to padded_scaled_video >> encode.

ffmpeg.StartInfo.Arguments = @"/c ffmpeg.exe -i " + VideoIn + " -i " + imageIn + " -filter_complex \"[0:v] scale='if(gt(a,6/4),480,-2)':'if(gt(a,6/4),-2,320)' [scaledv], [1:v] scale=iw*.5:ih*.5 [scaledicon], [scaledv] pad=480:320:(ow-iw)/2:(oh-ih)/2:black [scaledpaddedv], [scaledpaddedv][scaledicon] overlay=25:25,setsar=1:1 [finalv]\" -map \"[finalv]\" -map 0:a" + " -ss " + startCut.ToString() + " -c:v libx264 -preset slow -crf 20 -c:a aac -t " + duration.ToString() + " " + VideoOut + " -y -report";

Which works perfectly.

and then try to merge these videos with this command:

ffmpeg.StartInfo.Arguments = "/c ffmpeg.exe -f concat -safe 0 -i \"" + concatTextDirectory + "\" -c copy -vf \"subtitles=\\'" + videoInSrt + "\\':force_style='FontName=DejaVu Serif,FontSize=26'\" -c:v libx264 -preset slow -crf 20 -c:a aac " + videoOut + " -y -report";

Which again works ok if cutted video parts are from the same type of video, and if they aren't there will be no video and only audio or jittery video on non-similar video parts. The first cutted video part is dominant on other parts.

Question is most of the time video parts are not from the same video type and output is not playable. How could I cut video parts to be similar in properties to work without problem on concatenation part?

This is -report log output: https://drive.google.com/open?id=0B_kWzdnSIxuQY09QWWZGVTBPdHM


3 Answers 3


Glad you fixed your problem. For anyone else coming across the question...

The problem is that concat demuxer only works when multimedia streams are the same.

As described here, all files must have the same streams (same codecs, same time base, etc.).

So you have to use a filter, forcing a re-encode. See this reference.


I was going to comment on paulzag's comment, but I need 50 reputation (what a stupid system).

For me, the filter and demuxer weren't working at all, but I found out about "Concat protocol" on ffmpeg's site, which worked prefectly for me: https://trac.ffmpeg.org/wiki/Concatenate#protocol

I used the "intermediate files" method, which uses MPEG-2 TS files.

Previous to this, the video was freezing at the first insertion for about 3 seconds, and then playing the rest of the video at half FPS. I think it was due to the videos being encoded using different settings.

Now it perfectly combines both videos together with no issue.

From the site:

While the demuxer works at the stream level, the concat protocol works at the file level. Certain files (MPEG-2 transport streams, possibly others) can be concatenated. This is analogous to using cat on UNIX-like systems or copy on Windows.


The following command concatenates three MPEG-2 TS files and concatenates them without re-encoding:

ffmpeg -i "concat:input1.ts|input2.ts|input3.ts" -c copy output.ts

Using intermediate files

If you have MP4 files, these could be losslessly concatenated by first transcoding them to MPEG-2 transport streams. With H.264 video and AAC audio, the following can be used:

ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts 
ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4

Using named pipes to avoid intermediate files

If you're using a system that supports named pipes, you can use those to avoid creating intermediate files. This sends stderr (to which ffmpeg sends all the written data) to /dev/null, to avoid cluttering up the command-line:

mkfifo temp1 temp2 
ffmpeg -y -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp1 2> /dev/null & \
ffmpeg -y -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp2 2> /dev/null & \
ffmpeg -f mpegts -i "concat:temp1|temp2" -c copy -bsf:a aac_adtstoasc output.mp4 

The additional -y switch is needed to force ffmpeg to write to existing files temp1 and temp2, which are the named pipes. Without the switch, the first two ffmpeg programs running in the background will not produce any output because they wait for interactive yes/no answers to the questions whether to overwrite existing files.

All MPEG codecs (MPEG-4 Part 10 / AVC, MPEG-4 Part 2, MPEG-2 Video, MPEG-1 Audio Layer II, MPEG-2 Audio Layer III (MP3), MPEG-4 Part III (AAC)) are supported in the MPEG-TS container format, although the commands above would require some alteration (e.g., the -bsf bitstream filters will have to be changed).


End up using filters as this answer guides >> https://video.stackexchange.com/a/15479/10894

ffmpeg.StartInfo.Arguments = "/c ffmpeg.exe -i C:\\Users\\mtst\\Desktop\\Clips\\h\\a1.mp4 -i C:\\Users\\mtst\\Desktop\\Clips\\h\\a2.mp4 -i C:\\Users\\mtst\\Desktop\\Clips\\h\\a3.mp4 -filter_complex \"[0:v]setpts=PTS-STARTPTS[v0]; [0:a]asetpts=PTS-STARTPTS[a0]; [1:v]setpts=PTS-STARTPTS[v1]; [1:a]asetpts=PTS-STARTPTS[a1]; [2:v]setpts=PTS-STARTPTS[v2]; [2:a]asetpts=PTS-STARTPTS[a2]; [v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[v][a]\" -map \"[v]\" -map \"[a]\" -c:v libx264 -preset slow -crf 20 -c:a aac " + videoOut + " -y -report";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.