I'm creating a batch script to daily convert screen captures to video
Its all working except I cant get the screen caps to start at 0001 each day,so my current script fails.
Is there a way within FFMPEG to just read all the files in a folder (e.g. if I have one folder with 500 files starting with 01234.jpg and another folder with files starting at 87654.jpg)?
If not, can I use the batch script to find the start and finish points and send that to ffmpeg?
my script
#echo off
setlocal enabledelayedexpansion
rem Check if a new folder has been added to the watched folder
for /f "tokens=*" %%a in ('dir "Y:\fastone_timelapse" /B /AD') do (
set "new_folder=%%a"
rem Check if jpeg files are present in the new folder
if exist "Y:\ft\!new_folder!\*.jpg" (
ffmpeg.exe -f image2 -framerate 25 -pattern_type sequence -start_number 00001 -i "Y:\ft\!new_folder!\%%05d.jpg" -c:v libx265 -b:v 5000k -g 100 "Y:\ft\!new_folder!.mp4"
)
)
endlocal
if exist "Y:\ft\!new_folder!\*.jpg" (
set "first="
for /f %%y in ('dir /on /b /a-d "Y:\ft\!new_folder!\*.jpg"') do set "last=%%~ny"&if not defined first set "first=%%~ny"
echo First=!first! Last=!last!
ffmpeg.exe -f image2 -framerate 25 -pattern_type sequence -start_number 00001 -i "Y:\ft\!new_folder!\%%05d.jpg" -c:v libx265 -b:v 5000k -g 100 "Y:\ft\!new_folder!.mp4"
BTW, you don't appear to need new_folder as each appearance of !new_folder! can be replaced by %%a.
Related
I'm trying to create a bat file to create multiple gifs from videos and I'm doing ok so far.
I managed to create this command
#echo off
FOR /L %%A IN (0,5,3600) DO (ffmpeg -ss %%A -t 3 -i "%~1" -vf "fps=10,scale=360:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 -fs 1000000 "output %%~nxA.gif")
The problem is that I use a command for 1h videos, which will create 720 gifs
The problem is that when the video is under 1h of duration the bat file continues to run and create empty gif files, until reaches 720 gif files
I need to know what command should I use.
I've tried -abort_on empty_output, -abort_on empty_output_stream, and -xerror
but none of this worked
Also I'm using the for to loop the command, therefore I don't know if this is a ffmpeg problem or a for loop problem
Help me!
Instead of using the fixed 3600, I suggest you use ffprobe to get the video's duration.
Either through another for-loop...
#ECHO off
FOR /F "delims=" %%A IN ('ffprobe -v 0 -show_entries format^=duration -of compact^=p^=0:nk^=1 "%~1"') DO ^
FOR /L %%B IN (0,5,%%A) DO ^
ffmpeg -ss %%B -t 3 -i "%~1" -vf "fps=10,scale=360:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" "%~n1_%%B.gif"
...or with a variable...
#ECHO off
FOR /F "delims=" %%A IN ('ffprobe -v 0 -show_entries format^=duration -of compact^=p^=0:nk^=1 "%~1"') DO SET dur=%%A
FOR /L %%A IN (0,5,%dur%) DO ^
ffmpeg -ss %%A -t 3 -i "%~1" -vf "fps=10,scale=360:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" "%~n1_%%A.gif"
I have written a simple batch file to convert movies in a folder. The problem is, it never stops running. It keeps processing the next movie (which it just created). Here is the code:
#echo off & setlocal
FOR /r %%# in (*.mkv) DO IF NOT EXIST "%%~dpn#_265.mkv" (Title %~nx0 Processing: %%~f#
ffmpeg -i "%%~f#" -c:v libx265 -vtag hvc1 -map_metadata 0 -max_muxing_queue_size 1000 "%%~dpn#_265.mkv")
PAUSE
The file to convert is c:\tmp\c.mkv.
The code creates the first file c:\tmp\c_265.mkv, then creates a second file from that one called c:\tmp\c_265_265.mkv, and so on.
Your main issue with your code is likely to be that you're checking for the existence of every mkv file in the same directory, but with _265 appended to the basename. So in your example %%# would resolve to c:\tmp\c_265.mkv and you'd be checking for the existence of c:\tmp\c_265_265.mkv, which probably isn't the only check you needed to make. You probably wanted to check to ensure that the last four characters of %%# were not already _265 too.
Without changing your for /r methodology, (I'd go with a for /f with findstr idea similar to Gerhard's comment), you could do it like this:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /R "." %%G In (
"*.mkv"
) Do If /I "%%~xG" == ".mkv" If Not Exist "%%~dpnG_265%%~xG" (
Set "FileBaseName=%%~nG"
SetLocal EnableDelayedExpansion
If Not "!FileBaseName:~-4!" == "_265" (
"ffmpeg.exe" -i "%%G" -c:v libx265 -vtag hvc1 -map_metadata 0 -max_muxing_queue_size 1000 "%%~dpnG_265%%~xG"
)
EndLocal
)
Pause
I compress a video with FFMPEG by means of a batch scripting one-liner with a FOR LOOP. After compressing I want to remove the original file.
This code perfectly makes the compressing job, but the original file is not being removed:
for %%i in (*.mp4) do ffmpeg -i "%%i" -vcodec libx265 -crf 28 "%%~ni"-small.mp4&&del "%%i"
pause
What have I missed?
By the way, this is a near to perfect compression ratio for any kinds of educational videos, tutorials, etc. which have occupied your hard drive.
I would assume that this methodology will serve you better:
#For /F "EOL=? Delims=" %%G In ('Dir /B /A:-D "*.mp4" ^| ^
"%__AppDir__%findstr.exe" /IR "\.mp4$" ^| ^
"%__AppDir__%findstr.exe" /IRV "\-small\.mp4$"'
) Do #If Not Exist "%%~nG-small%%~xG" (
ffmpeg.exe -i "%%G" -vcodec libx265 -crf 28 "%%~nG-small%%~xG"
If Not ErrorLevel 1 If Exist "%%~nG-small%%~xG" Del /A /F "%%G")
The idea is to get a directory listing of all files with an .mp4 extension. Using the first findstr to exclude any files which match the short, (8.3), names. It then pipes those through another findstr search, to exclude any of those files which already have -small appended to their basename. Next it checks to make sure that there are no existing files within that directory, with the same basename plus the -small basename suffix, (preventing possible overwrites). If all of those pass, ffmpeg.exe should be invoked, (subject to ffmpeg.exe being in the current directory, within %PATH%, or registered as executable with its location in the registry). Once the conversion has completed, the script checks the returned ErrorLevel was less than 1, (to ensure it was successful), and if so, whether the new file, (with the -small basename suffix) exists. If so it will proceed with the deletion, using the Del command.
If you wanted to try it again using conditional operators instead of the ErrorLevel, then it would look like this:
#For /F "EOL=? Delims=" %%G In ('Dir /B /A:-D "*.mp4" ^| ^
"%__AppDir__%findstr.exe" /IR "\.mp4$" ^| ^
"%__AppDir__%findstr.exe" /IRV "\-small\.mp4$"'
) Do #If Not Exist "%%~nG-small%%~xG" (
ffmpeg.exe -i "%%G" -vcodec libx265 -crf 28 "%%~nG-small%%~xG" && (
If Exist "%%~nG-small%%~xG" Del /A /F "%%G"))
I just put the '/f' after DEL and it perfectly does the job. I don't know if there are any shortcomings of the forced deletion but it works.
A final line:
for %%i in (*.mp4) do ffmpeg -i "%%i" -vcodec libx265 -crf 28 "%%~ni"-small.mp4&&del /f "%%i"
pause
Im trying to convert all '.mp4' files in a folder into '.ts' files so I can use FFmpeg.exe to combine them all into one long '.mp4' video.
I have to use the concat command to combine the .ts files when using ffmpeg.
Below is a working line of code to do this the long way.... I want to use a for loop in case I have way more than just 3 files to combine.
ffmpeg.exe -hide_banner -y -i concat:"a.ts|b.ts|c.ts" -c copy -bsf:a aac_adtstoasc "COMBINED.mp4"
I can make this loop work by using the below for command but the last file found can not have a pipe after it | like c.ts" above didn't.
FOR /F "USEBACKQ TOKENS=* DELIMS= ,|" %%I IN ('%%~dpnG.ts') DO (
SET FNAME=%%~dpnI
ECHO.
ECHO !FNAME!
PAUSE>NUL
EXIT
)
Does anyone know if it's even possible (maybe with tokens) to do this in a batch file? If not any suggestions? PowerShell?
In response to Compo's question here is my working script:
#ECHO OFF
SETLOCAL
COLOR 0A
TITLE CONCAT MULTIPLE MP4 FILES
PUSHD "%~dp0"
SET FF="C:\MAB\local64\bin-video\ffmpeg.exe"
:: SET VIDEO NAME WITHOUT EXTENSION (.MP4)
SET IN01=a
SET IN02=b
SET IN03=c
SET COMBINED=FULL
:: CREATE TEMP .TS VIDEOS OF THE FILES YOU WANT TO COMBINE
%FF% -hide_banner -y -i "%IN01%.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts "%IN01%.ts"
%FF% -hide_banner -y -i "%IN02%.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts "%IN02%.ts"
%FF% -hide_banner -y -i "%IN03%.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts "%IN03%.ts"
:: COMBINE TEMP .TS FILES INTO COMBINED .MP4
%FF% -hide_banner -y -i concat:"%IN01%.ts|%IN02%.ts|%IN03%.ts" -c copy -bsf:a aac_adtstoasc "%COMBINED%.mp4"
ECHO.
PAUSE
EXIT
Here's an example script which, based upon my understanding, should do as you needed:
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
COLOR 0A
TITLE CONCAT MULTIPLE MP4 FILES
SET "FF=C:\MAB\local64\bin-video\ffmpeg.exe"
SET "COMBINEDBASENAME=FULL"
SET "CONCATLIST="
PUSHD "%~dp0"
FOR %%G IN (*.mp4) DO (
REM CREATE TEMP .TS VIDEOS OF THE FILES YOU WANT TO COMBINE
"%FF%" -hide_banner -y -i "%%~G" -c copy -bsf:v h264_mp4toannexb -f mpegts "%%~nG.ts"
IF NOT ERRORLEVEL 1 IF EXIST "%%~nG.ts" (
IF NOT DEFINED CONCATLIST (SET "CONCATLIST=%%~nG.ts") ELSE (
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%H In ("!CONCATLIST!|%%~nG.ts") DO ENDLOCAL & SET "CONCATLIST=%%~H"
)
)
)
IF NOT DEFINED CONCATLIST GOTO :EOF
REM COMBINE TEMP .TS FILES INTO COMBINED .MP4
"%FF%" -hide_banner -y -i concat:"%CONCATLIST%" -c copy -bsf:a aac_adtstoasc "%COMBINEDBASENAME%.mp4"
REM REMOVE :: FROM THE NEXT LINE TO DELETE THE .TS FILES
::DEL "%CONCATLIST:|=" "%"
ECHO=
PAUSE
GOTO :EOF
I'm trying to remove the first chapter of a TV show rip. The idea is to have something similar to netflix, where the first episode will show the intro then the consecutive episodes don't.
I've been getting the chapter time with ffprobe then doing it manually with this
for %%A IN (*.mp4) DO ffmpeg -ss <CHAPTERTIME> -i "%%A" -vcodec copy -acodec copy "tmp.mp4" && DEL "%%A" && RENAME "tmp.mp4" "%%A"
How do I pass the first chapter time code value so ffmepg knows where to seek to?
Also hoping to use the same the same value to offset the subtitles so they align after the cut.
Note: The machine I've doing the cutting on is windows but I can use my FreeBSD server if that's easier.
It's probably the worst way to do it, but atleast it works, still cant get it to work in batch.
#setlocal enableextensions enabledelayedexpansion
#echo off
for %%A IN (*.mp4) DO ffprobe -show_chapters -print_format flat "%%A" > out.txt
for /f "tokens=* delims=" %%a in ('findstr /C:"chapters.chapter.1.start_time=" out.txt') do (
set S=%%a
set S=!S:~30,20!
echo !S!
)
for %%A IN (*.mp4) DO ffmpeg -ss !S! -i "%%A" -vcodec copy -acodec copy "tmp.mp4" && DEL "%%A" && RENAME "tmp.mp4" "%%A"
endlocal