2

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.18 ... 01.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?

2
  • What OS are you using? Oct 8, 2015 at 9:39
  • Windows 8, 64-bit. Sorry, I forgot to mention that.
    – user---
    Oct 8, 2015 at 9:41

2 Answers 2

5

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.)

3
  • Backtick around filename.mp4 didn't work for me, but single quotes did. So use 'file1.mp4' instead of file1.mp4 if you run into 'unsafe file name' errors. Feb 4, 2018 at 16:10
  • I had to add -safe 0 before -i to solve the "unsafe file name" error. Got this from here: stackoverflow.com/questions/38996925/…
    – Alaa M.
    Jul 1, 2021 at 5:27
  • The last command to iterate through each subfolder wasn't working for me for a number of reasons, so I had to edit it to for /d /r %f in (*) do cd %~ff & ffmpeg -f concat -safe 0 -i filelist.txt -c copy merged.mp4
    – Tock
    Dec 7, 2023 at 0:24
0

I made this batch script if this still helps. https://github.com/jeromerobles/video_merge

1
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Destroy666
    Dec 31, 2023 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .