How to split video to 3 parts by ffmpeg? - batch-file

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

Related

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

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

how to make the script re-ask for the input again after failure?

this some script for ffmpeg that i added to windows context menu
so i just copy paste what i want done
when this .bat fails [wrong argument for instance ] it closes and i have to restart it
how to make the script re-ask for the input again after failure?
#echo off
echo -------------------------------------------------------------
echo FILTERS
echo -------------------------------------------------------------
echo,
echo VERTICAL FLIP= -lavfi vflip
echo HORIZONTAL FLIP= -lavfi hflip
echo NEGAT COLORS= -lavfi Enegate
echo NEGATE LUMINANCE= -lavfi lutyuv=y=negval
echo GRAYSCALE= -lavfi hue=s=0
echo ISOLATE COLOR= -lavfi colorhold=color="orange":similarity=0.29:blend=0
echo PS CURVES PRESET= -lavfi curves=psfile='MyCurvesPresets/purple.acv'
echo VIGNETTE EFFECT= -lavfi vignette=PI/4
echo LOOKUP TABLE= -lavfi lut3d=c\\:/nnn/ggg.cube
echo SPEED UP OR SLOW DOWN= -lavfi setpts=PTS/2
echo SPEED UP VIDEOS, AUDIOS= -lavfi "[0:v]setpts=PTS/2[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]"
echo REVERSE= -lavfi reverse
echo POSTERIZE= -lavfi elbg=l=8:n=1
echo MOTION BLUR= -lavfi tmix=frames=20:weights="10 1 1"
echo HARD SUB= -lavfi subtitles=s.ass
echo SOFT SUB= -scodec mov_text -metadata:s:s:0 language=eng
echo ONE IMAGE= -lavfi -frames 1
echo AN IMAGE EVERY 60 SEC= -lavfi fps=1/60
echo ONLY IFRAMES= -skip_frame nokey
echo GIF= -lavfi "fps=10,scale=320:-2:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0
echo STACKS= -lavfi "[0:v][1:v][2:v][3:v]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0[v]" -map "[v]"
echo EMBED THUMBNAIL = -i 1.jpg -map 0:0 -map 0:1 -map 1 -c:0 copy -c:v:1 png -disposition:v:1 attached_pic
ECHO,
echo -------------------------------------------------------------
echo CODEC OPTIONS
echo -------------------------------------------------------------
echo,
echo -lavfi "[0:v]scale=-2:720,setpts=PTS/1.5[v];[0:a]atempo=1.5[a]" -map "[v]" -map "[a]" -crf 40 -preset ultrafast
echo -map 0 -codec copy
echo -acodec copy -vcodec libx264 -vsync cfr -crf 20 -pix_fmt yuv420p -tune film -preset veryfast -movflags +faststart
echo -acodec aac -ac 2 -ab 128k -ar 44100 / -acodec libmp3lame -ab 320k -ar 44100 -id3v2_version 3
echo -qscale:v 2
echo,
the main part:
set /P extra="ENTER CODEC OPTIONS="
echo,
echo Processing "%~nx1"
echo Output will be "%~n1"_output.mp4"
ffmpeg -v error -stats -y -i "%~1" %extra% "%~n1"_output.mp4
pause

create file with ffmpeg by for?

I want
for %i in("%image%\*.jpg")for %%i in ("%INPUT%\*.mp3") DO ffmpeg -i %%i -i %i -filter_complex "scale=1280:720[v]" -map '[v]' -map '[a]' -shortest -c:v libx264 -c:a copy -preset superfast "%OUTPUT%/%%~ni.mp4"
but it not run.

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"

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

Resources