FFmpeg batch merge videos within a folder

0

I have a folder c:\myfolder\ containing a variable amount of mp4 files with same resolution / codec.

Now I need a .bat file to merge all videos into one.

e.g. c:\myfolder\1.mp4, c:\myfolder\2.mp4, c:\myfolder\3.mp4 into c:\myfolder\output.mp4

I've found a way to do this by creating a .txt file first which contains all input videos and to do in another step

ffmpeg.exe -f concat -i mylist.txt -c copy output.mp4

Question: Is there a way to do this in one step?

Dr. Snail

Posted 2018-06-18T07:18:44.730

Reputation: 247

A batch file could produce the .cue file for you, but I see a sorting problem if there are more than 9 files as 10.mp4 will follow 1.mp4 unless you use a counting for /l to build the list. – LotPings – 2018-06-18T14:22:56.680

@LotPings there is no sorting problem since the files have sortable timestamps - could you write your code into a answer? – Dr. Snail – 2018-06-19T09:15:18.030

Answers

1

For those who use bash (Linux/Mac), here is the script to generate the list of all files in a folder and then merge it all into one video file:

#!/bin/bash
for filename in pieces/*.mp4; do
  echo "file $filename" >> concat-list.txt
done

ffmpeg -f concat -i concat-list.txt merged.mp4

echo "Concatenated videos list:"
cat concat-list.txt
rm concat-list.txt

It takes all mp4 files from pieces/ folder and concatenates the videos into merged.mp4 file.

Files list is generated in alphabetical order, so if you want the videos to be in a particular order, name them like 01-murder-scene.mp4, 02-office-scene.mp4 etc.

DarthVanger

Posted 2018-06-18T07:18:44.730

Reputation: 111

See also https://trac.ffmpeg.org/wiki/Concatenate

– slhck – 2019-10-18T11:09:08.437

0

As I don't have ffmpeg installed on this pc, untested:

:: Q:\Test\2018\06\20\SU_1332169.cmd
@Echo off
Set "BaseDir=c:\myfolder"
Set "OutMp4=%BaseDir%\Output.mp4"
Set "FfmpegCue=%Temp%\Ffmpeg.Cue"

:: gather files but exclude evt. present output file
( For /f "Delims=" %%A in (
  'Dir /B/S/A-D/ON "%BaseDir%\*.mp4" 2^>NUL ^|findstr /VLI "%OutMp4%" '
  ) Do Echo=%%A
) > "%FfmpegCue%"

:: Just to show what's in the cue file:
more "%FfmpegCue%"
Pause

:: Do the concat
ffmpeg.exe -f concat -i "%FfmpegCue%" -c copy "%OutMp4%" && (
  Echo Successfully created "%OutMp4%"
  choice /M "Delete %FfmegCue% "
  If not Errorlevel 2 Del "%FfmegCue%"
) || (echo ffmpeg exited with error)

LotPings

Posted 2018-06-18T07:18:44.730

Reputation: 6 150