ftgp
ftgp

Reputation: 21

ffmpeg/for loop: How to make ffmpeg or cmd loop stops after it founds an error?

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!

Upvotes: 0

Views: 283

Answers (1)

Reino
Reino

Reputation: 3423

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"

Upvotes: 1

Related Questions