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 )
+ Reply to Thread
Results 1 to 4 of 4
-
Last edited by Budman1; 7th Apr 2018 at 01:22. Reason: Give credit for original script
-
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
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%
Similar Threads
-
Batch trim using FFMPEG
By taonas in forum EditingReplies: 4Last Post: 25th Feb 2021, 10:17 -
Applications to trim/join videos without losing any quality?
By immortal192 in forum EditingReplies: 9Last Post: 13th Aug 2018, 14:23 -
Is it possible to grab videos off Youtube from a selected trim timeline?
By likwid8 in forum Video Streaming DownloadingReplies: 2Last Post: 17th Jan 2017, 05:08 -
Problem with multiple using of 'Trim' filter in Avisynth
By Balbes in forum Video ConversionReplies: 6Last Post: 19th Dec 2015, 07:15 -
Trim MV4 Files, Front and Back - Batch
By poleary200 in forum EditingReplies: 19Last Post: 12th Dec 2013, 13:29