A simple script to join files using ffmpeg. This script works in Linux only.
Save the following code as a script file. For example, save as joinvideos.sh and set the executable permission with chmod 755 joinvideos.sh. Then copy all videos that need to be joined in a single folder containing the script. The script will join any video files it finds in the folder. The script will also join the video files in the alphabetical order of their names. So it is important to rename the files as f1.mp4, f2.mp4, f3.mp4 if the original filenames have no naming order.
#!/bin/bash # joinvideos.sh v1.5 by Saugata # join multiple video files with ffmpeg # remove temp files if they exist from interrupted past run if ls temp-*.mp4 1> /dev/null 2>&1; then echo "Removing older temp*.mp4 files" rm temp-*.mp4 fi # make a list of all mp4 files in the diretory # except the output.mp4 if it exists listofvidfiles=`ls -1 | grep mp4 | grep -v "output"` for f in $listofvidfiles; do # check for location of Video stream # if Video is at Stream #0:1 then swap to Stream #0:0 # save file as temp file vidstreamno=`ffprobe $f 2>&1 | grep Video | cut -d":" -f2 | cut -d"(" -f1` if [ "$vidstreamno" -eq "1" ]; then ffmpeg -i $f -map 0:1 -map 0:0 -c copy temp-$f else cp $f temp-$f fi # if Video is at Stream #0:0 then copy it to a temp file echo $f: "Video stream at" $vidstreamno done # create a text files from list of temp-*.mp4 for ffmpeg input ls -1 *.mp4 | grep mp4 | grep temp | sed "s/^/file '/" | sed "s/$/\'/" > vidfilelist.txt echo "-----------------------------" echo "Joining files ..............." cat vidfilelist.txt echo "-----------------------------" # use ffmpeg to join mp4 files ffmpeg -f concat -safe 0 -i vidfilelist.txt -c copy output.mp4 # clean temp files before exit if ls temp-*.mp4 1> /dev/null 2>&1; then echo "Removing temp*.mp4 files" rm temp-*.mp4 fi