VideoHelp Forum
+ Reply to Thread
Results 1 to 4 of 4
Thread
  1. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    Recently I had a request for a batch file that would trim a certain length (3 minutes in this case) from a folder of videos were of varying lengths but always have the last 3 minutes cut off. (Removing credits???) Anyway I altered a script originally from an unknown author on Stack Overflow and created a batch file and tested on several files. Although this is an unusual need, it is difficult if movies are of unknown and different lengths and maybe it will help others who need it.

    I used MediaInfoCLI.exe and MediaInfo.dll in the folder with the videos to pull the duration in time format and ran that following .BAT file from that folder. If the file is unexpectedly less than 3 minutes, the CMD window throws an error and does not copy the file but continues on for the rest of the files. All videos are copied with codec copy and have OUT appended to the name so the originals are never overwritten, but it is always a good idea to use copies before you edit...

    Code:
    @echo off
    setlocal enabledelayedexpansion
    for %%z in (*.mp4,*.flv) do (
    mediainfo --Inform=General;%%Duration/String3%%:\r\n %%z > Duration.txt
    echo video %%z
    setlocal enabledelayedexpansion
    
    rem Get start time:
    set TrmTime="00:03:00.000"
    for /F "tokens=1-4 delims=:.," %%a in (!TrmTime!) do (
    set /A "start=((((%%a*3600)+%%b*60)+%%c)*1000)+%%d" 
    echo start !start!
    )
    
    rem Any process here...
    
    rem Get end time:
    for /F "tokens=1-4 delims=:.," %%a in ('findstr /I ":" "Duration.txt"') do (
    set /A "end=((((%%a*3600)+%%b*60)+%%c)*1000)+%%d" 
    echo end !end! 
    )
    
    rem Get elapsed time:
    set /A "elapsed=end-start"
    echo elapsed !elapsed!
    
    Rem Show_elapsed_time:
    set /A "hh=elapsed/(60*60*1000)"
    set /A "rest=!elapsed! %%(60*60*1000)"
    set /A "mm=!rest! /(60*1000)"
    set /A "rest=!elapsed! %%(60*1000)"
    set /A "ss=!rest!/1000"
    set /A "cc=!rest! %%1000"
    
    echo !hh!:!mm!:!ss!.!cc!
    set "time=!hh!:!mm!:!ss!.!cc!"
    echo time !time!
    
    ffmpeg -i %%z -to !time! -c copy out%%~nxz
    )
    Last edited by Budman1; 7th Apr 2018 at 01:22. Reason: Give credit for original script
    Quote Quote  
  2. hey budman1 it's giving me "%%z: No such file or directory" the error is happening when the script reach the line "ffmpeg -i %%z -to !time! -c copy out%%~nxz" any idea why?
    i have the video file in the bin called 1.mp4, thanks
    Quote Quote  
  3. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    okay. Tried this with another folder and got the same results. I modified the script for relative locations of files. Note the changes in red below.

    Code:
    @echo off
    setlocal enabledelayedexpansion
    for %%z in (*.mp4,*.flv) do (
    mediainfo --Inform=General;%%Duration/String3%%:\r\n %%z > "%%~pnz.txt"
    echo video %%z
    setlocal enabledelayedexpansion
    
    rem Get start time:
    set TrmTime="00:03:00.000"
    for /F "tokens=1-4 delims=:.," %%a in (!TrmTime!) do (
    set /A "start=((((%%a*3600)+%%b*60)+%%c)*1000)+%%d" 
    echo start !start!
    )
    
    rem Any process here...
    
    rem Get end time:
    for /F "tokens=1-4 delims=:.," %%a in ('findstr /I ":" "%%~pnz.txt"') do (
    set /A "end=((((%%a*3600)+%%b*60)+%%c)*1000)+%%d" 
    echo end !end! 
    )
    
    rem Get elapsed time:
    set /A "elapsed=end-start"
    echo elapsed !elapsed!
    
    Rem Show_elapsed_time:
    set /A "hh=elapsed/(60*60*1000)"
    set /A "rest=!elapsed! %%(60*60*1000)"
    set /A "mm=!rest! /(60*1000)"
    set /A "rest=!elapsed! %%(60*1000)"
    set /A "ss=!rest!/1000"
    set /A "cc=!rest! %%1000"
    
    echo !hh!:!mm!:!ss!.!cc!
    set "time=!hh!:!mm!:!ss!.!cc!"
    echo time !time!
    
    ffmpeg -i "%%z" -to !time! -c copy out%%~nxz
    )
    Put all the files including CMD, MediaInfo CLI, video in the same folder. You can drag and drop a file and it will run all of the MP4, FLV files in the folder, or just run the CMD File
    Out put should look like this:

    C:\Users\Bud\Desktop\test 1>"C:\Users\Bud\Desktop\test 1\Trim.cmd"
    video CGOP.mp4
    start 180000
    end 477478
    elapsed 297478
    0:4:57.478
    time 0:4:57.478
    ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers
    built with gcc 9.1.1 (GCC) 20190807
    configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfi
    g --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libb
    luray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enab
    le-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --e
    nable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable
    -libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 -
    -enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enab
    le-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --en
    able-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --en
    able-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --e
    nable-dxva2 --enable-avisynth --enable-libopenmpt
    libavutil 56. 31.100 / 56. 31.100
    libavcodec 58. 54.100 / 58. 54.100
    libavformat 58. 29.100 / 58. 29.100
    libavdevice 58. 8.100 / 58. 8.100
    libavfilter 7. 57.100 / 7. 57.100
    libswscale 5. 5.100 / 5. 5.100
    libswresample 3. 5.100 / 3. 5.100
    libpostproc 55. 5.100 / 55. 5.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'CGOP.mp4':
    Metadata:
    major_brand : isom
    minor_version : 512
    compatible_brands: isomiso2avc1mp41
    title : NAME CHANGED DUE TO R RATING
    encoder : Lavf57.21.101
    comment : Recorded and Tagged by Replay Media Catcher 7 (www.applian
    .com)
    Duration: 00:07:57.48, start: 0.000000, bitrate: 497 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x270 [
    SAR 1:1 DAR 16:9], 396 kb/s, 29.97 fps, 29.97 tbr, 11988 tbn, 59.94 tbc (default
    )
    Metadata:
    handler_name : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, flt
    p, 91 kb/s (default)
    Metadata:
    handler_name : SoundHandler
    File 'outCGOP.mp4' already exists. Overwrite ? [y/N] y
    Output #0, mp4, to 'outCGOP.mp4':
    Metadata:
    major_brand : isom
    minor_version : 512
    compatible_brands: isomiso2avc1mp41
    title : NAME CHANGED DUE TO R RATING
    comment : Recorded and Tagged by Replay Media Catcher 7 (www.applian
    .com)
    encoder : Lavf58.29.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x270 [
    SAR 1:1 DAR 16:9], q=2-31, 396 kb/s, 29.97 fps, 29.97 tbr, 11988 tbn, 11988 tbc
    (default)
    Metadata:
    handler_name : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, flt
    p, 91 kb/s (default)
    Metadata:
    handler_name : SoundHandler
    Stream mapping:
    Stream #0:0 -> #0:0 (copy)
    Stream #0:1 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    frame= 8918 fps=0.0 q=-1.0 Lsize= 18021kB time=00:04:57.47 bitrate= 496.3kbits
    /s speed=3.31e+03x
    video:14372kB audio:3344kB subtitle:0kB other streams:0kB global headers:0kB mux
    ing overhead: 1.725460%
    video outCGOP.mp4
    start 180000
    end 297565
    elapsed 117565
    0:1:57.565
    time 0:1:57.565
    ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers
    built with gcc 9.1.1 (GCC) 20190807
    configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfi
    g --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libb
    luray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enab
    le-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --e
    nable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable
    -libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 -
    -enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enab
    le-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --en
    able-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --en
    able-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --e
    nable-dxva2 --enable-avisynth --enable-libopenmpt
    libavutil 56. 31.100 / 56. 31.100
    libavcodec 58. 54.100 / 58. 54.100
    libavformat 58. 29.100 / 58. 29.100
    libavdevice 58. 8.100 / 58. 8.100
    libavfilter 7. 57.100 / 7. 57.100
    libswscale 5. 5.100 / 5. 5.100
    libswresample 3. 5.100 / 3. 5.100
    libpostproc 55. 5.100 / 55. 5.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'outCGOP.mp4':
    Metadata:
    major_brand : isom
    minor_version : 512
    compatible_brands: isomiso2avc1mp41
    title : NAME CHANGED DUE TO R RATING
    encoder : Lavf58.29.100
    comment : Recorded and Tagged by Replay Media Catcher 7 (www.applian.com)
    Duration: 00:04:57.57, start: 0.000000, bitrate: 496 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x270 [
    SAR 1:1 DAR 16:9], 395 kb/s, 29.97 fps, 29.97 tbr, 11988 tbn, 59.94 tbc (default
    )
    Metadata:
    handler_name : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, flt
    p, 92 kb/s (default)
    Metadata:
    handler_name : SoundHandler
    Output #0, mp4, to 'outoutCGOP.mp4':
    Metadata:
    major_brand : isom
    minor_version : 512
    compatible_brands: isomiso2avc1mp41
    title : NAME CHANGED DUE TO R RATING
    comment : Recorded and Tagged by Replay Media Catcher 7 (www.applian
    .com)
    encoder : Lavf58.29.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x270 [
    SAR 1:1 DAR 16:9], q=2-31, 395 kb/s, 29.97 fps, 29.97 tbr, 11988 tbn, 11988 tbc
    (default)
    Metadata:
    handler_name : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, flt
    p, 92 kb/s (default)
    Metadata:
    handler_name : SoundHandler
    Stream mapping:
    Stream #0:0 -> #0:0 (copy)
    Stream #0:1 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    frame= 3526 fps=0.0 q=-1.0 Lsize= 6418kB time=00:01:57.56 bitrate= 447.2kbits/s speed=3.92e+03x
    video:4955kB audio:1339kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.970840%
    Quote Quote  
  4. Thanks a lot ! <3
    Quote Quote  



Similar Threads

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