Batch file: How to play with loops and creating new directories - loops

I can't find the solution.
I made the directory to be generated, with the approach that if it exists, create another one with -1, -2 ... added to the name of the directory.
But I can't get it to continue exporting the loop from MP4list and export it differently in the other directories.
Below I will explain the batch file in great detail, maybe someone has a solution how to approach the situation:
First try
#echo off
setlocal enabledelayedexpansion
set "musicdir=MUZICA-wav-mp3"
set "exportfoldername=Canal"
set "exportdir=Export\%exportfoldername%
set "videodir=Video-15-sec"
set n=0
md "%exportdir%"||call :a %n%
<"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%-%n%\%%~nA.mp4"
del mergedmp4.mp4"
)
)
exit
:a
set /a n+=1
md "%exportdir%-%n%"||goto a
exit /b
For my second try I used for /f "skip=
#echo off
setlocal enabledelayedexpansion
set "musicdir=D:\MONTAJ VIDEO\video-simplu\MUZICA-wav-mp3"
set "exportdir=D:\MONTAJ VIDEO\video-simplu\Export\canal-1
set "exportdir2=D:\MONTAJ VIDEO\video-simplu\Export\canal-2
set "exportdir3=D:\MONTAJ VIDEO\video-simplu\Export\canal-3
set "videodir=D:\LUCRU-VIDEO\Video-15-sec"
rem ============== Canal-1
for /f "skip=1" %%G IN (mp4list.txt) DO if not defined line set "line=%%G"
for %%A in ("%musicdir%\*") DO (
mkdir "%exportdir%"
echo %line%
ffmpeg -f concat -safe 0 -i "%line%" -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"
)
rem ============== Canal-2
for /f "skip=3" %%G IN (mp4list.txt) DO if not defined line set "line2=%%G"
for %%A in ("%musicdir%\*") DO (
mkdir "%exportdir2%"
echo %line2%
ffmpeg -f concat -safe 0 -i "%line2%" -c copy mergedmp4.mp4
ffmpeg -i mergedmp4.mp4 -i "%%A" -map 0:v -map 1:a -vcodec copy -c:a aac -b:a 320k "%exportdir2%\%%~nA.mp4"
del mergedmp4.mp4"
)
rem ============== Canal-3
for /f "skip=5" %%G IN (mp4list.txt) DO if not defined line set "line3=%%G"
for %%A in ("%musicdir%\*") DO (
mkdir "%exportdir3%"
echo %line3%
ffmpeg -f concat -safe 0 -i "%line3%" -c copy mergedmp4.mp4
ffmpeg -i mergedmp4.mp4 -i "%%A" -map 0:v -map 1:a -vcodec copy -c:a aac -b:a 320k "%exportdir3%\%%~nA.mp4"
del mergedmp4.mp4"
)
I really don't know in what direction to go...
Let's say I have 5 songs in the directory musicdir and 100 lines with different .mp4 paths in mp4list.txt , 100 directories with the same 5 songs in each directory should be created. Canal-1, Canal-2 ... Canal-100
When all songs in musicdir are exported in Canal-1 the loop will start again with the same songs from musicdir, with the difference that .mp4 videos will be exported in the next folder Canal-2 and so on.
but it is very important that the loop continues in the file mp4list.txtand continue where it left off in the previous directory in order not to repeat the same mp4 files
mp4list.txt contains the path's of mp4 files, all are unique
even if from musicdir it will start from the beginning with exporting mp4 files in a new directory, in mp4list.txt it will continue where it left off in the previous directory
Batch will finish when all lines in mp4list.txt are finished.
mp4list.txt is like this
output.txt
output1.txt
...
output100.txt
and every output.txt have the path to mp4 file.
like this
file '1.mp4'
file '2.mp4'
file '3.mp4'
file '4.mp4'
thank you for your help

Related

How to rename lots of files with several dots in them and making them a new suffix with file extension?

Hello I have a problem where ffmpeg produces a filename where it just ads another .mp4
I do not want to change the ffmpeg script as It is a part of another script so I decided that I would rather make another script which its whole purpose would be just to rename these files from "something_something".mp4.mp4 to "something_something"_SmallFile.mp4
Also if I use this code where the script and files are nested in directory like so:
E:\Folder1\folder2\folder3\folder4
than the script stops working entirely, but if it is nested just one folder above it starts working again. Strange.
for /d %%X in (*) do rename *.txt *.cmd
pause
Note that this is just a test script to test if it works at all.
My current code is:
for /f "tokens=* delims=" %a in ('dir /b "%FilesLocation%"') do if %~xa EQU .mp4.mp4 ren "%a" "%~na_SmalFile.mp4"
EDIT:
There is code for ffmpeg:
SET /A ffmpeg_qv=24
for /R %%A in (*.mp4, *.avi, *.mov, *.wmv, *.ts, *.m2ts, *.mkv, *.mts) do (
echo Processing "%%A"
ffmpeg -hwaccel auto -i "%%A" -pix_fmt nv12 -map 0:v -map 0:a -map_metadata 0 -c:v h264_amf -gops_per_idr 1 -rc cqp -qp_p %ffmpeg_qv% -qp_i %ffmpeg_qv% -b:v 0K -c:a aac -b:a 384k -movflags +faststart -movflags use_metadata_tags "%%A.mp4"

How to batch process entire folder with ffmpeg? a batch script that will scan currently run folder

I have this code but it is not working
I want batch script to scan currently run directory and batch process the MP4 files in that directory
But first i want it to create a new directory called as newfiles and put the processed files in that folder with the same name
Please help me to fix this batch script ty
md "%~dp0\newfiles"
for %%a in (%~dp0\"*.mp4") do ffmpeg -i "%%a" -c:v libx264 -preset veryslow -crf 18 -c:a aac -b:a 192k -pix_fmt yuv420p "newfiles\%~dp0%%a.mp4"
pause
The code is not creating a new directory not batch process the mp4 files in that directory (the directory that is batch script executed)
ok i have solved the question as below
md "%~dp0\newfiles"
for %%a in (%~dp0\"*.mp4") do ffmpeg -i "%%a" -c:v libx264 -preset veryslow -crf 18 -c:a aac -b:a 192k -pix_fmt yuv420p "newfiles\%%~na.mp4"
pause
Here is a fix for your script, as requested in your question.
#Echo Off
If Not Exist "%~dp0*.mp4" Exit /B
CD /D "%~dp0"
If Not Exist "newfiles\" MD "newfiles"
For %%A In (*.mp4) Do ffmpeg -i "%%A" -c:v libx264 -preset veryslow -crf 18 -c:a aac -b:a 192k -pix_fmt yuv420p "newfiles\%%A"

Ignore files based on filename in Batch Script

for %%a in ("*.*") do ffmpeg -i "%%a" -c:v libx265 -threads 4 -preset medium -
crf 23 -c:a aac -b:a 128k "%%~na.mp4"
I have this script to automatically compress video files in using the x265 codec. Sometimes the compressed files end up being larger than the source files. In those cases I will affix _OPTI at the end of the filename.
example
Original:
1.mp4
Renamed: 1_OPTI.mp4
My question how can batch script filter out the _OPTI files in the folder that has _OPTI in the filename and ignore them when it adds files in the compression queue?
Try something like this in a batch file
setlocal enableextensions
for /F "usebackq delims=" %%a in (`dir /b *.mp4 ^| findstr /V OPTI`) do (
echo ffmpeg -i "%%a" -c:v libx265 -threads 4 -preset medium -crf 23 -c:a aac -b:a 128k "%%~na.mp4"
)

Error when trying to convert mp3 (and image) to mp4 using ffmpeg SPACES IN NAME

I found how to batch convert MP3s to MP4s using the code below in a batch file:
for %%f in (*.mp3) do ffmpeg -i %%f -loop 1 -i image.jpg -c:a copy -c:v libx264 -shortest %%~nf.mp4
It works fine UNLESS the FILENAME has spaces in it: FILENAME.mp3 works fine but FILE NAME.mp3 gives the error "No such file or directory."
I tried inserting the "delims=" before and after the %%f but it keeps saying that it is unexpected. Any ideas?
Thanks.
for %%f in (*.mp3) do ffmpeg -i "%%f" -loop 1 -i image.jpg -c:a copy -c:v libx264 -shortest "%%~nf.mp4"
In general "quote filenames that may contain spaces"
"delims=" can only be used with for /f as in
for /f "delims=" %%f in (...
Where the porpose of the for is primarily to read lines from the files.
use 'dir /b /a-d filemask' - including the quotes to provide for /f with a listing of filenames to process. The /b means "basic form" (names only) and /a-d means "no directories".
If your filenames contain separators like spaces, use `"delims=" otherwise the implicit "tokens=1" will truncate the value applied at the first separator.
Thank you everyone for your help, the quotes are what did the trick.
I changed this:
for %%f in (*.mp3) do ffmpeg -i %%f -loop 1 -i image.jpg -c:a copy -c:v libx264 -shortest %%~nf.mp4
To this:
for %%f in ("*.mp3") do ffmpeg -i "%%f" -loop 1 -i image.jpg -c:a copy -c:v libx264 -flags global_header -pix_fmt yuv420p -shortest "%%~nf.mp4"
FYI: Extra toggles were added but had nothing to do with the spaces causing issues:
-flags global_header -pix_fmt yuv420p

How to split video to 3 parts by ffmpeg?

I want to split video to 3 parts in batch mode. So, I have this code:
#echo off
for %%i in ("*.mp4") do ffmpeg -i "%%i" -vcodec copy -acodec copy -ss 00:00:00 -t 00:00:05 D:\Ebook\%%~nxi_1.mp4
for %%i in ("*.mp4") do ffmpeg -i "%%i" -vcodec copy -acodec copy -ss 00:00:05 -t 00:00:10 D:\Ebook\%%~nxi_2.mp4
for %%i in ("*.mp4") do ffmpeg -i "%%i" -vcodec copy -acodec copy -ss 00:00:10 D:\Ebook\%%~nxi_3.mp4
pause
It works but having some problem:
1. It save file: .mp4_1.mp4. How to remove ".mp4" ?
2. When filename of video contain space character. It shows error: "Invalid argument". How to fix it ?
Thank you very much !
what about this:
#echo off
for %%i in (*.mp4) do (
ffmpeg -i "%%~i" -vcodec copy -acodec copy -ss 00:00:00 -t 00:00:05 "D:\Ebook\%%~nxi_1.mp4"
ffmpeg -i "%%~i" -vcodec copy -acodec copy -ss 00:00:05 -t 00:00:10 "D:\Ebook\%%~nxi_2.mp4"
ffmpeg -i "%%~i" -vcodec copy -acodec copy -ss 00:00:10 "D:\Ebook\%%~nxi_3.mp4"
)
pause

Resources