BASH Script for ffmpeg to easily merge video files

Joined
Feb 8, 2016
Messages
77
Reaction score
27
Country
United States
I know I've been slacking lately with my custom dash cam and have been playing with footage from my existing dash cams. One problem I found is a lot of dash cams split the video into 1 minute segments. If you want to combine the videos into a single video, you have to manually put them into a video editing program and then combine them.

I solved this problem by creating a simple BASH script that will combine the videos in a given folder into a single video and then also create a separate 10x video. I created this on Ubuntu Linux. Ffmpeg can be easily installed with apt-get. This script was meant for .MP4 files, but it can be easily modified for other formats.

Once ffmpeg is installed, you just need to put this script in the folder with the video files you want to combine and then run it:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# 1.) Find MP4 files and export them to list.txt with the full path
ls -d $PWD/*.MP4 > list.txt

# 2.) Edit the list.txt file to add file ' to the beginning and ' to the end
sed -i "s|$PWD|file '$PWD|g" list.txt
sed -i "s|.MP4|.MP4'|g" list.txt

# 3.) Combine all the videos into a single video
ffmpeg -f concat -safe 0 -i list.txt -c copy combined.mp4

# 4.) Speed up the video 10x. -an for removing audio so video is correct length
ffmpeg -i combined.mp4 -filter:v "setpts=0.1*PTS" -an spedup.mp4

# 5.) Cleanup list.txt
rm list.txt
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
My preference for file size on my dash cams is about 1 minute and I wish I had the option on my mobius to select that as the length. When i do need to merge two or more subsequent clips or some times make a compilation of several different events - I use ffmpeg as it can merge and produce a target file in seconds.

There one problem with ffmpeg -f concat is files in the reference text file (which you called list.txt) needs to be the same encoding. This can be a problem when
  1. Combining two or more dashcam models (My A119 has different encoding from Mobius and A118c2)
  2. If I do editing on some but no all of the files, such as changing the speed, or cropping.
The solution to this problems are
  1. Encode all of the files to be the same encoding prior to producing the list text file
  2. Do bogus encoding on files, such as If I crop one video to be 1/2 screen, I encode the other clips to be 2/2
I will provide a script or batch file where I solved this issues. For now other ways of producing a reference file used by ffmpeg

If using windows
On Windows Command-line:
Code:
(for %i in (*.mp4) do @echo file '%i') > mylist.txt

Or for Windows Powershell:
Code:
foreach ($i in Get-ChildItem .\*.mp4) {echo "file '$i'" >> mylist.txt}

Or for Windows bat-file:
Code:
(for %%i in (*.mp4) do @echo file '%%i') > mylist.txt

For the above, change from mp4 to the extension of the source files.

Here is a script for with a while loop in linux
Code:
#mkinput.sh
rm input.txt
#for f in `ls *.AVI *.avi`
#for f in `ls *.MKV *.mkv`
#for f in `ls *.WEBM *.webm`
#for f in `ls *.mp3 *.MP3 `
#for f in `ls *.MP4 *.mp4 `
for f in `ls *.MOV *.mov`
do
    echo $f
    echo file_ " " \' $f \' $out $nf |sed 's/ //g' |sed 's/file_/file /g' >> input.txt
done

ffmpeg -y -f concat -safe 0 -i input.txt -c copy output.mov

After producing the above with a text editor, if you call the bash file mkinput.sh
Code:
bash mkinput.sh

The above is for MOV files. If the files are NOT MOV, put a comment in front of the line and remove the comment. Also change extension of output.mov to the extension of camera or clip files.

Here is a link to the ffmpeg wiki on the topic. https://trac.ffmpeg.org/wiki/Concatenate. In fact, I cut and paste the windows commands from there as I am currently on by Linux box.

Edit the wiki has a more direct way of producing a reference file compared to my script.
Code:
# with a bash for loop
for f in ./*.wav; do echo "file '$f'" >> mylist.txt; done
# or with printf
printf "file '%s'\n" ./*.wav > mylist.txt
 
Thanks for adding this. I just switched over to Windows for my desktop from Linux so I'll definitely use this. My script itself was for the same type of files from the same dash cam. That's all I was playing with then. I can see what you provided helping if I start playing with multiple dash cams.
 
Thread starter Similar threads Forum Replies Date
adorfer Managing / Editing Videos 3
Back
Top