Here's a script for Windows' cmd.exe that doesn't use ffprobe (only uses ffmpeg):
@echo off
setlocal
if "%~1"=="" (
echo Usage: %~n0 input_video_file seconds_to_remove
goto :eof
)
set "input=%~1"
set "x=%~2"
set "output=%~n1_trimmed%~x1"
REM Get duration from ffmpeg output
for /f "tokens=1,2 delims=, " %%a in ('ffmpeg -i "%input%" 2^>^&1 ^| findstr /i "Duration"') do (
set duration=%%b
)
echo Duration string: %duration%
REM Use PowerShell to get total duration in seconds
for /f "usebackq delims=" %%d in (powershell.exe -NoProfile -Command "[TimeSpan]::Parse('%duration%').TotalSeconds") do set "DURATION=%%d"
echo Total duration: %DURATION% seconds
REM Calculate new duration
for /f "usebackq delims=" %%e in (powershell.exe -NoProfile -Command "%DURATION% - %x%") do set "NEW_DURATION=%%e"
echo New duration: %NEW_DURATION% seconds
REM Run ffmpeg to trim the video
ffmpeg -i "%input%" -t %NEW_DURATION% -c copy "%output%"
echo The output file "%output%" has been created without the last %x% seconds.
Example of use:
script.bat input.mp4 7
will remove the last 7 seconds in input.mp4: it'll create a new file input_trimmed.mp4
If one uses this script on videos that have telemetry information (e.g., GoPro GPMF), replace ffmpeg -i "%input%" -t %NEW_DURATION% -c copy "%output%" with:
REM Run ffmpeg to trim the video
ffmpeg -i "%input%" -t %NEW_DURATION% -map 0:v -map 0:a -map 0:3 -copy_unknown -tag:2 gpmd -c copy "%output%"
(the extra attributes -map 0:v -map 0:a -map 0:3 -copy_unknown -tag:2 gpmd keeps the telemetry information)