Ffmpeg assistance

Looking for some assistance with ffmpeg.

I have a set of mp4 files that are not the same length (and I will have more in the future). I wrote a scritp (well, I copied a script from the interwebs and played with some ffmpeg documentation) that i put in a .bat file. When executed, it takes all of the files in the directory that are mp4 and trims the first 5 seconds off the file and moves the cut files into a separate directory with the exact same name. Fine and dandy.

I want to do the same thing but be able to cut the first 5 seconds AND the last 5 seconds. That, I have read, is not nearly as easy. I finally found a script that is supposed to work (looping through all of the files in the directory that match the extension given) but when I run it, it just blinks the CLI screen and then doesn’t do anything. I am sure I am doing something wrong, but I can’t figure it out. This will run in Windows and so, I believe it needs to be a .bat or .cmd file.

Ideally, the script would have a few parameters
L* ocation of ffmpeg executable

  • Extension
  • Seconds to cut at the beginning
  • Seconds to cut at the end
  • Location to put trimmed/cut files

Can anyone help? I know there are folks who know all of the bells and whistles and I have been trying to work on this for weeks and have finally given up.

Anyone?

Have you looked at this?

Thanks. I have seen that but I am in no way a programmer and I couldn’t get things to work the way it needed. I would like to combine them all in one so I can just put the files into the folder, launch the bat/cmd/?? file and then leave it to do its thing as it loops through. Not only could I not get that to work, but I couldn’t get just the ffprobe to work correctly.
I have worked on this for so long, it is like I have a mental block now.

Perhaps this will help you with your goal.

@ECHO OFF
SetLocal
FOR /F "delims=" %%I IN ('ffprobe.exe -v 0 -show_entries format^=duration -of compact^=p^=0:nk^=1 MyMP4File.mp4 2^>^&1') DO SET "Duration=%%I"
ECHO Current Duration is: %Duration%
SET /a Duration=Duration-5
ECHO Duration minus 5sec: %Duration%
ffmpeg.exe -i MyMP4File.mp4 -ss 00:05:00 -to %Duration% -c copy MyMP4FileTrimmed.mp4
EndLocal
PAUSE

Thanks! I will try this later this evening and see if I can make it work.

It kind of worked (thank you). It took off the last five seconds but it also took off the first 5 MINUTES. Also, in my previous script, it just read whatever was in the directory with the right extension and did them all one at a time and then looped through to the next and so on and placed the finished files with the exact same name just in a different folder. it looked like

set ffmpeg=“C:\ffmpeg\bin\ffmpeg.exe”
for %%f in (“*.mp4”) do ffmpeg -i “%%f” -ss 00:00:05.000 -c:v copy -c:a copy “U:\To Cut\Trimmed%%f”

I can’t figure out how to put your part into my part.

the reason it took off the first 5 minutes is because of the 00:05:00 in the last line before EndLocal and Pause, just move the middle 05 to the end like so 00:00:05 and that should correct the issue…

At least that’s what I think… I am no good with batch files, but that seems to be what the issue is

My bad…
Messed up the time index. Oops… Should of been 00:00:05 and not 00:05:00

Your script only does *mp4 files. I’ll use that.
Here is the complete script. Modify to your needs…

@ECHO OFF

2>NUL MD ".\To Cut"

SET FileTypes=*.mp4


ECHO.
ECHO Searching for file type(s): %FileTypes%

FOR %%F IN (%FileTypes%) DO SET /a Total+=1
ECHO There are a total of %Total% file(s) in the current folder.
ECHO.
ECHO.

SET StartTime=%TIME%

ECHO.

FOR /F "delims=" %%M IN ('DIR ^/b %FileTypes%') DO CALL:ExecuteCommand "%%M"

ECHO TimeStart: %StartTime%
ECHO TimeEnd  : %TIME%

ECHO.
ECHO.

ECHO Completed . . . .&PAUSE&EXIT

:ExecuteCommand
SET /a Processed+=1
ECHO Processing %Processed% of %Total%...

FOR /F "delims=. tokens=1" %%P IN ('ffprobe.exe -v 0 -show_entries format^=duration -of compact^=p^=0^:nk^=1 %1') DO SET Duration=%%P
SET /a Duration=Duration-5
ECHO Saving . . . . & ECHO  "%~n1Trimmed.mp4"
ffmpeg.exe -ss 00:00:05 -to %Duration% -i %1 -c copy ".\To Cut\Trimmed-%~n1.mp4" -hide_banner -loglevel warning


ECHO.
ECHO.
GOTO:EOF
2 Likes

This, with just a very few minor tweaks works beautifully! Thank you very much!!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.