FFMPEG Merged video files (lossless)

preface

Recently, I have been doing video transcoding related business, but the video source supplier gave some scattered video clips of DVD, so I need to merge the clips and transcode the business by myself. This article mainly records the process of video merging and common merging methods.

The environment

  • FFMPEG BUILDS 4.4.1

    FFmpeg Builds

  • GeForce GTX 1060 (NVIDIA)

    List of graphics card support

  • windows 10

  • OVB video file

    VTS_01_1.VOB VTS_01_2.VOB VTS_01_3.VOB VTS_01_4.VOB VTS_01_5.VOB

Video mergeway

Concat separator
newmerge.txt
file 'VTS_01_1.VOB'
file 'VTS_01_2.VOB'
file 'VTS_01_3.VOB'
file 'VTS_01_4.VOB'
file 'VTS_01_5.VOB'
Copy the code
Merging video files
ffmpeg -f concat -safe 0 -i merge.txt -c copy vts.mp4
Copy the code

PS: Do not add -safe 0 if the relative path is used

File list generation techniques

If there are too many video files, the merge.txt copy is cumbersome, so you can use the command to quickly generate file list files.

windows

(for %i in (*.VOB) do @echo file '%i') > merge.txt
Copy the code

PS: *.VOB indicates the video file suffix.

linux

# bash 
for f in *.wav; do echo "file '$f'" >> merge.txt; done
# printf
printf "file '%s'\n" *.wav > merge.txt
Copy the code
Concat agreement
disadvantages

This method is used in a relatively small range, only suitable for MPEG and other video formats;

Merge video
ffmpeg -i "concat:input1.ts|input2.ts|input3.ts" -c copy merge.ts
Copy the code
expand

If it is another format file, you can transcode before merging, for example

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 -movflags +faststart output.mp4
Copy the code

PS: -movFlags + fastStart Files generated using these two attributes can be viewed below.

Concat filter(hurt)
instructions

Filters are suitable for synchronizing segments of video and audio streams. All segments must have the same number of streams of each type and will also be the number of streams at output time.

Merge video
ffmpeg -i input1.mp4 -i input2.webm -i input3.mov \
-filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mkv
Copy the code

PS: The input streams are input1.mp4, input2.webm and input3.mov, and then tell FFMPEG to pull out the video stream and the audio stream from the video, and use concat filter to tell it there are three video streams, each with one video stream and one audio stream. Connect and generate a new video stream [outV] and an audio stream [outA], then map the new video stream and audio stream to a new file output.mkv.

conclusion

The above is the use of FFMPEG video merge method and process, each method has advantages and disadvantages, recommend concAT separator > CONCAT protocol > CONCAT filter, can not say which method is best, can only choose according to the actual situation, if you have a better way or suggestion, welcome to discuss.

\