Home > database >  ffmpeg - concatenate mp4 files audio from a folder structure
ffmpeg - concatenate mp4 files audio from a folder structure

Time:09-18

I use this batch file to concatenate several .mp4 files and then join them with an audio song.

@echo off

mkdir ".\Export" 
set "sourcedir="  
set "outputdir=Export" 

for %%i in (*.mp4) do echo file '%%i'>> lista.txt
ffmpeg -f concat -safe 0 -i lista.txt -c copy mergedmp4.mp4

for %%F in (*.wav *.mp3) DO ( 
    ffmpeg -i mergedmp4.mp4 -i "%%F" -map 0:v -map 1:a -vcodec copy -c:a aac -b:a 320k "%outputdir%\%%~nF.mp4"
)

del lista.txt
del mergedmp4.mp4

So far so good, It's working

I would describe what will help me more and I need to happen :

I have this folder structure \Video-15-sec\Canal-1\Videos-1, Videos-2 .. Videos-15 or sometimes can be until Videos-20 or more, depends how I organize them.

All of Videos folders have .mp4 files in them and are renamed all the time1.mp4, 2.mp4, 3.mp4 ... 25.mp4 ... 35.mp4 ..etc each of the Videos folders respect the name of file starting with 1.mp4 to how many are in the folder.

The directory structure is like this:



──Video-15-sec
│       ├───Canal-1
│       │   ├───Videos-1 (.mp4 files in it)
│       │   ├───Videos-2 (.mp4 files in it)
│       │   ├───Videos-3 (.mp4 files in it)
│       │   ├───Videos-4 ...
│       │   ├───Videos-5
│       │   ├───Videos-6
│       │   ├───Videos-7
│       │   ├───Videos-8
│       │   ├───Videos-9
│       │   ├───Videos-10
│       │   ├───Videos-11
│       │   ├───Videos-12
│       │   ├───Videos-13
│       │   ├───Videos-14
│       │   └───Videos-15
│       ├───Canal-2
│       │   ├───Videos-1 (.mp4 files in it)
│       │   ├───Videos-2 (.mp4 files in it)
│       │   ├───Videos-3 (.mp4 files in it)
│       │   ├───Videos-4 ...
│       │   ├───Videos-5
│       │   ├───Videos-6
│       │   ├───Videos-7
│       │   ├───Videos-8
│       │   ├───Videos-9
│       │   ├───Videos-10
│       │   ├───Videos-11
│       │   ├───Videos-12
│       │   ├───Videos-13
│       │   ├───Videos-14
│       │   └───Videos-15
│       ├───Canal-3
│       │   ├───Videos-1 (.mp4 files in it)
│       │   ├───Videos-2 (.mp4 files in it)
│       │   ├───Videos-3 (.mp4 files in it)
│       │   ├───Videos-4 ...
│       │   ├───Videos-5
│       │   ├───Videos-6
│       │   ├───Videos-7
│       │   ├───Videos-8
│       │   ├───Videos-9
│       │   ├───Videos-10
│       │   ├───Videos-11
│       │   ├───Videos-12
│       │   ├───Videos-13
│       │   ├───Videos-14
│       │   └───Videos-15
│       ├───Canal-4

I want to have a separate folder \allmusic where I will have all the music, and the batch file will always export with the name of the song.

all the mp4 files of each separate folder Videos-1, Videos-2 ... Videos-10..., will be concatenated separately and then joined with a single song, each separate folder, with one song each. All the songs and Videos folders in loop, until all my songs are done.

The video created will be exported in a separate folder \Export-all-Videos

Actions of Batch file:

─Video-15-sec
│       ├───Canal-1
│       │   ├───Videos-1 (concatenate.mp4 files from)   1 song -> Folder  \Export-all-Videos\name of the song.mp4
│       │   ├───Videos-2 (concatenate.mp4 files from)   1 song -> Folder  \Export-all-Videos\name of the song.mp4
│       │   ├───Videos-3 (concatenate.mp4 files from)   1 song -> Folder  \Export-all-Videos\name of the song.mp4
│       │   ├───Videos-4 ...
│       │   ├───Videos-5
│       │   ├───Videos-6
│       │   ├───Videos-7

I hope someone has a solution for this.

Thank you very much

CodePudding user response:

I found a solution,

it works for me, with the mention that very, very rarely it does not go to the next line in mp4list.txt and videos are duplicated with different audio file from musicdir.

if someone can improve it, I would be very happy.

@echo off
setlocal enabledelayedexpansion
set "musicdir=MUZICA-wav-mp3"  
set "exportdir=Export" 

REM here are the mp4 files
set "videodir=Video-15-sec"  

<"mp4list.txt" (
    for %%A in ("%musicdir%\*") DO (
        set /p mp4list=
        ffmpeg -f concat -safe 0 -i "!mp4list!" -c copy mergedmp4.mp4
    ffmpeg -i mergedmp4.mp4 -i "%%A" -map 0:v -map 1:a -vcodec copy -c:a aac -b:a 320k "%exportdir%\%%~nA.mp4"
        del mergedmp4.mp4"
    )
)

mp4list.txt contain the path to the txt files with the mp4 path file inside, ready to be concatenate.

D:\ ... folder1\output.txt   
D:\ ... folder2\output.txt
D:\ ... folder3\output.txt
.....
D:\ ... folder20\output.txt

and output.txt it's like this"

file '1.mp4'
file '2.mp4'
file '3.mp4'
file '4.mp4'
....
file '15.mp4'

thank you again.

  • Related