VideoHelp Forum




+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 30 of 32
  1. Member Ygramul's Avatar
    Join Date
    Jan 2007
    Location
    Poland
    Search Comp PM
    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:

    Code:
    \FFmpeg was unexpected at this time.
    Then I made ChatGPT make corrections and it did the following:

    Code:
    Removed quotes from set ffmpeg_path=...
    
    Wrapped "%ffmpeg_path%" and "%ffprobe_path%" with quotes only when used.
    https://pastebin.com/6tJyDnjE

    But this script doesn't work, either:
    Code:
    'C:\Program' is not recognized as an internal or external command, operable program or batch file.
    What am I doing wrong?
    Quote Quote  
  2. 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
    -t needs to equal the length of the video - 10 seconds. in HH:MM:SS

    Maybe you can apply this to a script.
    Quote Quote  
  3. Member Ygramul's Avatar
    Join Date
    Jan 2007
    Location
    Poland
    Search Comp PM
    Originally Posted by cholla View Post
    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
    -t needs to equal the length of the video - 10 seconds. in HH:MM:SS

    Maybe you can apply this to a script.
    Thanks, but the whole point is that I have 700+ files and each has a different length.
    Quote Quote  
  4. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    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
    It's possible to pass input parameters to ffmpeg in straight seconds, rather than 00:00:00.000
    Cheers

    EDIT: p.s. Your profile says you're running Windows XP SP2. I tested on Win11
    Last edited by pcspeak; 25th Jun 2025 at 00:13.
    Quote Quote  
  5. Member Ygramul's Avatar
    Join Date
    Jan 2007
    Location
    Poland
    Search Comp PM
    Originally Posted by pcspeak View Post
    EDIT: p.s. Your profile says you're running Windows XP SP2. I tested on Win11
    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.
    Quote Quote  
  6. 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
    Quote Quote  
  7. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Originally Posted by Ygramul View Post
    I'm not sure, but I think that setting the path for ffmpeg.exe in the script doesn't work without that.
    . You're right. I keep ffmpeg, ffprobe & ffplay in
    Code:
    Path=C:\WINDOWS\system32
    The batch file works for me.
    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.
    Quote Quote  
  8. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Originally Posted by cholla View Post
    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
    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.cmd
    Code:
    @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
    Comment out the line 'del tmp.txt'. Check to see if tmp.txt has any content.
    Last edited by pcspeak; 25th Jun 2025 at 14:29.
    Quote Quote  
  9. 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 (
    call rocess "%%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
    Quote Quote  
  10. 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.
    Quote Quote  
  11. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Originally Posted by cholla View Post
    The only problem I had is the total video duration does not show the 10 seconds off at the end.
    I just tested again. It most definitely works here.
    Try chopping off 60 seconds. If that fails then I'm lost as to what is different between our methods.
    Quote Quote  
  12. @ 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.
    Image Attached Files
    Quote Quote  
  13. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Video1.mp4 has a keyframe every 250 frames.
    @29.97fps this is every 8.33 seconds.
    ffmpeg cuts at the nearest keyframe. Each cut could differ from your requirements by as much as 4 seconds.
    (approximately. I've used rounding. )
    Quote Quote  
  14. Originally Posted by pcspeak View Post
    Video1.mp4 has a keyframe every 250 frames.
    @29.97fps this is every 8.33 seconds.
    ffmpeg cuts at the nearest keyframe. Each cut could differ from your requirements by as much as 4 seconds.
    (approximately. I've used rounding. )
    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
    It is spot on including duration.
    Image Attached Files
    Quote Quote  
  15. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    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.
    Quote Quote  
  16. 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.
    Quote Quote  
  17. @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
    Quote Quote  
  18. Member Ygramul's Avatar
    Join Date
    Jan 2007
    Location
    Poland
    Search Comp PM
    Originally Posted by videoAI View Post
    @cholla
    I'm still getting

    Code:
    \FFmpeg was unexpected at this time.
    and that's it, nothing else happens. What does that error even mean?
    Quote Quote  
  19. @ 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.
    Quote Quote  
  20. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Originally Posted by Ygramul View Post
    I'm still getting
    Code:
    \FFmpeg was unexpected at this time.
    and that's it, nothing else happens. What does that error even mean?
    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
    Post the content of log.txt here, wrapping the text with the CODE tags. ( # symbol in the formatting menu)
    Quote Quote  
  21. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    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!
    Quote Quote  
  22. Originally Posted by cholla View Post
    @ 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.
    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.
    Quote Quote  
  23. Upload, using code tags, this part from your code:

    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"
    Originally Posted by Ygramul View Post
    Originally Posted by videoAI View Post
    @cholla
    I'm still getting

    Code:
    \FFmpeg was unexpected at this time.
    and that's it, nothing else happens. What does that error even mean?
    As always .. there is nothing wrong with my environment
    Quote Quote  
  24. Originally Posted by pcspeak View Post
    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
    Post the content of log.txt here, wrapping the text with the CODE tags. ( # symbol in the formatting menu)
    .........
    As always .. there is nothing wrong with my environment
    Quote Quote  
  25. Member
    Join Date
    Mar 2008
    Location
    United States
    Search Comp PM
    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
    Quote Quote  
  26. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Originally Posted by davexnet View Post
    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.
    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.

    Originally Posted by davexnet View Post
    Code:
     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%
    Originally Posted by pcspeak View Post
    Post #4: It's possible to pass input parameters to ffmpeg in straight seconds, rather than 00:00:00.000
    "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.
    Quote Quote  
  27. 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?
    Quote Quote  
  28. Member Ygramul's Avatar
    Join Date
    Jan 2007
    Location
    Poland
    Search Comp PM
    Originally Posted by videoAI View Post
    Upload, using code tags, this part from your code:

    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"
    It's exactly the same in my script.

    Originally Posted by aramkolt View Post
    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?
    It's all supposed to be keyframe based, yes. Re-encoding all those files would take ages, so I don't have any interest in that.
    Quote Quote  
  29. Originally Posted by pcspeak View Post
    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
    Post the content of log.txt here, wrapping the text with the CODE tags. ( # symbol in the formatting menu)
    Where in the batch script is the code above?
    Where did the 1024 come from?

    Originally Posted by pcspeak View Post
    Code:
    ffmpeg -ss 30 -to 1211 -i video_in.mp4 -c copy -y video_out.mp4
    works, and is easier to code.
    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"
    [/QUOTE]

    @ 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%"
    Originally Posted by davexnet View Post
    Code:
    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%"
    Originally Posted by _Al_ View Post
    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.
    Adding pause to videoAI's batch file did not work.
    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.
    Originally Posted by Ygramul View Post
    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
    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.
    Quote Quote  
  30. Member
    Join Date
    Mar 2008
    Location
    United States
    Search Comp PM
    If the source is typical consumer video with temporal compression (log GOP) at least the start cut point will not be
    accurate, unless the ffmpeg video "copy" is doing some kind of "smart" rendering (just re-encode the initial GOP) - and I don't think it is.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!