Windows – Batch merge sets of video files

batchmergevideowindows

I have 158 folders, each containing 5-15 video files that are each a few minutes long. I would like to merge all of the videos into one video per folder, and delete the rest.

The folders are named as dates in a certain year with gaps between them (01.11, 01.12, 01.13, 01.14, 01.1801.28, 02.01, etc.), and the files in them are named as follows:

ds_AAAAA_BB_CCC_480.mp4

The first blank (AAAAA) is a number that is the same for all files in that folder. For folder 01.11, the number is 03075, and it goes up by one for each folder after that.

The second blank (BB) is the order of the files, from 01 to however many files are in that folder.

The third blank (CCC) is a set of three letters that varies per file and can be ignored.

For a couple examples, the first file in the first folder (01.11) is ds_03075_01_nws_480.mp4. The last (7th) file in the fourth folder (01.14) is ds_03078_07_zen_480.mp4.

Ideally, I'd like the new files to be in a separate folder (just to make it easy to delete the old files), and named in a format like 01_11.mp4, 01_12.mp4, and so on, but any naming convention there would be fine.

Does anyone have any suggestions as to how to go about this?

Best Answer

I've been merging (concatenating) videos lately, and I found this answer very useful. To start with, install ffmpeg and make sure it's on your path.

You have to make a list of files for each dir that consists of a text file in this format:

file `file1.mp4`
file `file2.mp4`
file `file3.mp4`

Then you just run ffmpeg with this syntax:

ffmpeg -f concat -i filelist.txt -c copy merged.mp4

I was constructing these by hand for a few files, but for 158 folders you'll want some automation. The approach I'd take is probably using the FOR command in the Windows command prompt. You can generate the file lists using something like:

for /r %f in (*.mp4) do echo file '%~nxf' >>%~dpf\concat.txt

This recursively (/r) scans the MP4 files in the current folder and all subfolders, and echoes (prints) the name and extension (that's the nx bit) to a separate file in each subfolder (the dp bit gets the drive letter and path). The >> bit makes it append the filename to the list, so if you run this twice you'll get a duplicated list. So delete any concat.txt files before running the command.

Then you want to run ffmpeg on each of those. You'll need something like:

for /d %f in (*) do cd %~ff & ffmpeg -f concat -i concat.txt -c copy ..\merged\%~nf.mp4

For each directory (/d), change into it (cd), and (&) run ffmpeg, generating a result file that goes in the "merged" folder of the top-level folder, and is named after the folder name.

Not tested, so use at your own risk. (Although it won't delete or overwrite anything without asking.)

Related Question