Let's say I have ten .mp4 files that vary in length. I want to remove the first 30 and the last 10 seconds from each of them and save the output in a folder called "trimmed". I made ChatGPT write the script because the part about using ffprobe to calculate the removal of the last 10 seconds is too complicated for me. Unfortunately, I keep getting errors. This is version 1:
https://pastebin.com/ykLk6Awn
The result is this error:
Then I made ChatGPT make corrections and it did the following:Code:\FFmpeg was unexpected at this time.
https://pastebin.com/6tJyDnjECode:Removed quotes from set ffmpeg_path=... Wrapped "%ffmpeg_path%" and "%ffprobe_path%" with quotes only when used.
But this script doesn't work, either:
What am I doing wrong?Code:'C:\Program' is not recognized as an internal or external command, operable program or batch file.
+ Reply to Thread
Results 1 to 30 of 32
-
-
I do not know how to do this with a script like in the link.
If you want to do this with each file separately this will work:
Code:ffmpeg -i input.mp4 -ss 00:00:30 -t -c copy C:\trimmed\output.mp4
Maybe you can apply this to a script. -
-
This requires:
https://mediaarea.net/download/binary/mediainfo/25.04/MediaInfo_CLI_25.04_Windows_x64.zip as well as ffmpeg.exe
Change the path in the batch file to suit the correct location.
trim-start-end-v01.cmd
Code:@echo off set mediainfo=C:\MediaInfoCLI\mediainfo.exe if not exist trimmed\ md trimmed for %%a in ("*.mp4", "*.mkv") do call :process "%%a" goto :end :process set video=%~1 "%mediainfo%" "--Output=Video;%%Duration%%" "%video%" > tmp.txt set /p Duration= < tmp.txt echo WScript.Echo eval(%Duration% / 1000) > eval.vbs for /f "tokens=1 delims=." %%a in ('cscript.exe //nologo eval.vbs %%a') do set /a length=%%a-10 ffmpeg.exe -ss 30 -to %length% -i "%~1" -c copy -y "trimmed\%~1" set Duration= set length= del tmp.txt goto :eof :end pause
Cheers
EDIT: p.s. Your profile says you're running Windows XP SP2. I tested on Win11Last edited by pcspeak; 25th Jun 2025 at 00:13.
-
Haha, I didn't even notice the Windows XP thing. I just realized that I registered in 2007, so I must've been using that OS then.
Thanks for the script, but it doesn't cut the last 10 seconds. I assume that it's the part that says length=%%a-10
that's responsible for this. I changed it to 50 just to test it and it doesn't make a difference.
By the way, there's one detail I completely missed because I seldom use ffmpeg and that's setting it as a system environment variable. I'm not sure, but I think that setting the path for ffmpeg.exe in the script doesn't work without that.Last edited by Ygramul; 25th Jun 2025 at 10:44.
-
I wanted to play with this but I'm not very experienced in using a script like this.
What location do I use for the input files?
These are the errors I got:
Invalid duration for option to: %length%
Error parsing options for input file %~1.
Error opening input files: Invalid argument -
. You're right. I keep ffmpeg, ffprobe & ffplay in
Code:Path=C:\WINDOWS\system32
When using -c copy, ffmpeg will trim at the nearest keyframe (I-frame), which might not be exactly where you specified. I've had videos with keyframes 5 seconds apart.
For more precise trimming you will probably have to re-encode each video. Or use another program, maybe. -
The batch file goes in with the videos. Start with 1 video in the folder.
As Ygramul stated you may need to add the path to ffmpeg.exe to the batch file.
trim-start-end-v02.cmdCode:@echo off set mediainfo=C:\MediaInfoCLI\mediainfo.exe set ffmpeg_path="C:\Program Files (x86)\FFmpeg 6.0\bin\ffmpeg.exe" if not exist trimmed\ md trimmed for %%a in ("*.mp4", "*.mkv") do call :process "%%a" goto :end :process set video=%~1 "%mediainfo%" "--Output=Video;%%Duration%%" "%video%" > tmp.txt set /p Duration= < tmp.txt echo WScript.Echo eval(%Duration% / 1000) > eval.vbs for /f "tokens=1 delims=." %%a in ('cscript.exe //nologo eval.vbs %%a') do set /a length=%%a-10 "%ffmpeg_path%" -ss 30 -to %length% -i "%~1" -c copy -y "trimmed\%~1" set Duration= set length= del tmp.txt goto :eof :end pause
Last edited by pcspeak; 25th Jun 2025 at 14:29.
-
The provided batch script has a few issues that need to be addressed:
1. The `setlocal` command should be used to ensure that environment variables are local to the script.
2. The `endlocal` command should be used to end the localization of environment variables.
3. The `goto :eof` command should be used to exit the script properly.
4. The `for /f` loop should handle the duration correctly.
5. The `ffmpeg` command should be correctly formatted to handle the input and output files.
Here is the corrected version of the script:
```batch
@echo off
set ffmpeg_path="C:\Program Files (x86)\FFmpeg 6.0\bin\ffmpeg.exe"
set ffprobe_path="C:\Program Files (x86)\FFmpeg 6.0\bin\ffprobe.exe"
md trimmed
for %%a in ("*.mp4") do (
for /f "tokens=* usebackq" %%b in (`"%ffprobe_path%" -v error -show_entries format^=duration -of default^=noprint_wrappers^=1:nokey^=1 "%%a"`) do (
callrocess "%%a" %%b
)
)
goto :eof
rocess
setlocal
set file=%~1
set duration=%~2
rem Remove fractional seconds
for /f "tokens=1 delims=." %%d in ("%duration%") do set /a new_end=%%d - 10
rem Format the new duration in HH:MM:SS
set /a hh=new_end / 3600
set /a mm=(new_end %% 3600) / 60
set /a ss=new_end %% 60
rem Pad with zeros
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
set end_time=%hh%:%mm%:%ss%
echo Trimming %file% from 00:03:00 to %end_time%
"%ffmpeg_path%" -i "%file%" -ss 00:03:00 -to %end_time% -c:v copy -c:a copy "trimmed\%%~nxa"
endlocal
goto :eof
```
### Explanation of Changes:
1. **Localization of Environment Variables**: The `setlocal` command is used at the beginning of the `rocess` label to ensure that environment variables are local to the script. The `endlocal` command is used at the end to end the localization.
2. **Correct Handling of File Paths**: The `ffmpeg` command now correctly handles the input and output file paths by using double quotes around the file names.
3. **Correct Handling of Duration**: The `for /f` loop correctly handles the duration by removing fractional seconds and formatting the new duration in HH:MM:SS format.
4. **Proper Exit**: The `goto :eof` command is used to exit the script properly.
This script should now correctly trim the specified duration from each `.mp4` file in the directory and save the trimmed files in the `trimmed` folder.As always .. there is nothing wrong with my environment -
I figured out where to put the video file or files.
In the MediaInfo_CLI folder.
I do not have ffprobe in the Environment path but I do have ffmpeg in it.
So I will put ffprobe there also.
The only problem I had is the total video duration does not show the 10 seconds off at the end.
It does show the 30 seconds off the beginning.
@ VideoAI, I copied the script you posted & it did not do anything.
Maybe if you post it in code tags I can do it better.
Like pcspeak did.Last edited by cholla; 25th Jun 2025 at 15:33.
-
-
@ pcspeak,
I did not do the script exactly as you suggested in the last post.
I cut 7 seconds off the beginning & 12 off the end.
The cuts were made but the duration time is incorrect.
The first video is the original. Video1.mp4
It shows 7:35 for the total duration.
The second is the cut video.Video2.mp4
It shows 7:28 for the total duration.
It ends about 7:16 total duration.
The 7:35 minus 19 seconds is 7:16. -
I appreciate the math.
When I use ffmpeg with code direct.
I get this result: with ffmpegcut.mp4
Code:ffmpeg -i C:\Users\USER\Desktop\Temp\sweet_emotion.mp4 -ss 00:00:07 -to 00:07:23 -c copy C:\Users\USER\Desktop\Work\ffmpegcut.mp4
-
Good. In the last video, every frame is an keyframe, so all cuts will be frame accurate.
it makes for a larger file, but great for editing.
Cheers. -
The ffmpegcut.mp4 is smaller that the other two.
The OP posted he has around 700 files to do.
All different lengths.
This is why he needs a batch script that will detect the length & make the cuts accordingly.
The scripts posted did not do this for me.
But I'm just trying to learn & playing around.
The one videoAI posted did not work for me at all. -
@cholla
Here you go ..
Code:@echo off set ffmpeg_path="C:\Program Files (x86)\FFmpeg 6.0\bin\ffmpeg.exe" set ffprobe_path="C:\Program Files (x86)\FFmpeg 6.0\bin\ffprobe.exe" md trimmed for %%a in ("*.mp4") do ( for /f "tokens=* usebackq" %%b in (`"%ffprobe_path%" -v error -show_entries format^=duration -of default^=noprint_wrappers^=1:nokey^=1 "%%a"`) do ( call :Process "%%a" %%b ) ) goto :eof :Process setlocal set file=%~1 set duration=%~2 rem Remove fractional seconds for /f "tokens=1 delims=." %%d in ("%duration%") do set /a new_end=%%d - 10 rem Format the new duration in HH:MM:SS set /a hh=new_end / 3600 set /a mm=(new_end %% 3600) / 60 set /a ss=new_end %% 60 rem Pad with zeros if %hh% lss 10 set hh=0%hh% if %mm% lss 10 set mm=0%mm% if %ss% lss 10 set ss=0%ss% set end_time=%hh%:%mm%:%ss% echo Trimming %file% from 00:03:00 to %end_time% "%ffmpeg_path%" -i "%file%" -ss 00:03:00 -to %end_time% -c:v copy -c:a copy "trimmed\%%~nxa" endlocal goto :eof
As always .. there is nothing wrong with my environment -
-
@ videoAI ,
I just get a quick flash of the command prompt.
Too fast to read.
No file created.
I did change the path to ffmpeg & ffprobe to match my OS. -
My apologies for partially hijacking your thread.
Please, post your batch file here for us to look at.
Also the full screen output from the batch file.
I find the easiest way to do that is to tack 2> log.txt to the end of the ffmpeg command.
e.g.Code:ffmpeg.exe -ss 30 -to 1024 -i "%~1" -c copy -y "trimmed\%~1" 2> log.txt
-
Just a quick note:-
I try to keep in mind that there may be new VideoHelp Forum members, and novices to batch files, reading these posts.
So, I may tend to over-explain myself.
Na! -
videoAI wrote what was all wrong with posted batch script, (most likely there was nothing wrong with it) and unfortunately took away "pause" command from the end of script. That part causes cmd prompt to freeze, if there was an error (or not), so you can actually read what went wrong.
-
As always .. there is nothing wrong with my environment
-
-
I found that by copying ffprobe.exe and ffmpeg.exe into the same folder as the bat file and video files,
it simplifies things for those new to BAT file scripting. (Myself included)
I disabled the "echo off" and added a "Pause" to allow examination of the script execution.
Code:rem @echo off set ffmpeg_path1=ffmpeg.exe set ffprobe_path1=ffprobe.exe rem md trimmed for %%a in ("*.mp4") do ( for /f "tokens=* usebackq" %%b in (`%ffprobe_path1% -v error -show_entries format^=duration -of default^=noprint_wrappers^=1:nokey^=1 "%%a"`) do ( call :Process "%%a" %%b ) ) pause goto :eof :Process setlocal set file=%~1 set duration=%~2 rem Remove fractional seconds for /f "tokens=1 delims=." %%d in ("%duration%") do set /a new_end=%%d - 10 rem Format the new duration in HH:MM:SS set /a hh=new_end / 3600 set /a mm=(new_end %% 3600) / 60 set /a ss=new_end %% 60 rem Pad with zeros if %hh% lss 10 set hh=0%hh% if %mm% lss 10 set mm=0%mm% if %ss% lss 10 set ss=0%ss% set end_time=%hh%:%mm%:%ss% echo Trimming %file% from 00:03:00 to %end_time% %ffmpeg_path1% -i "%file%" -ss 00:00:03 -to %end_time% -c:v copy -c:a copy "trimmed\%file%" endlocal goto :eof
-
That's why I suggested redirecting the output from ffmpeg to log.txt. One then has a hard copy to look back on. At lease until the next run.
"ffmpeg -ss 30 -to 1211 -i video_in.mp4 -c copy -y video_out.mp4" works, and is easier to code.Last edited by pcspeak; 27th Jun 2025 at 04:14.
-
Just checking, do the above methods not re-encode the video in any way? So in other words, do they just create a new key frame at the "new" beginning if there isn't already one there and the rest is a copy of the original?
-
-
Where in the batch script is the code above?
Where did the 1024 come from?
Where did the now 1211 come from?
This part of your batch file has "legnth".
This is missing is videoAI's & davexnet's batch files.
How would their batch files determine "legnth" ?
Code:for /f "tokens=1 delims=." %%a in ('cscript.exe //nologo eval.vbs %%a') do set /a length=%%a-10 "%ffmpeg_path%" -ss 30 -to %length% -i "%~1" -c copy -y "trimmed\%~1"
@ davexnet.You batch script actually worked pretty well.
I made these modifications specific to the file I was testing with:
Code:for /f "tokens=1 delims=." %%d in ("%duration%") do set /a new_end=%%d - 12 echo Trimming %file% from 00:00:00 to %end_time% %ffmpeg_path1% -i "%file%" -ss 00:00:07 -to %end_time% -c:v copy -c:a copy "trimmed\%file%"
It did not pause.
In regards to the OP's questions.
I do not see how removing a set amounts off the beginning & end will be correct for all 700 files.
I had to make individual settings for the test file I was working with.
At least to get it where I believe it should be.
If I remove 30 seconds off the beginning & 10 off the end of the test file I used.
The beginning would be missing too much & the end not enough.
Much less for 10 or 700 files
I do not believe there is a one size fits all solution.
Unless a batch script can be written that detects & removes "no Audio" & "no Video" from the beginning.
Then the same for the end.
Similar Threads
-
Video Freezing Issue at the Start of Clips Cut with ffmpeg
By doganjr in forum EditingReplies: 16Last Post: 5th Dec 2024, 04:32 -
Ffmpeg add images to MP4 video, one in front, one in end
By zydjohn in forum Newbie / General discussionsReplies: 1Last Post: 23rd Dec 2021, 02:45 -
FFMPEG transcoded MPEG-TS files are lagging to start video
By dogmydog in forum Video ConversionReplies: 19Last Post: 12th Apr 2021, 09:01 -
How to add/save 'start' and 'end' points on multiple parts of video (VLC) ?
By aaajan in forum Software PlayingReplies: 2Last Post: 7th Mar 2021, 06:10 -
ffmpeg cut off first 10 seconds of video then add intro 15 seconds in
By PeterWall in forum Newbie / General discussionsReplies: 2Last Post: 30th Jul 2020, 06:18