Merge scripts

Discussion in 'Scripting' started by °ツ, Jun 7, 2015.

  1. °ツ

    °ツ MDL Addicted

    Jun 8, 2014
    569
    599
    30
    #1 °ツ, Jun 7, 2015
    Last edited by a moderator: Apr 20, 2017
    I have 2 scripts that I use to cut 20 seconds at start and 20 seconds at the end of every video in a folder, using ffmpeg.

    For cutting the start of video I use:

    Code:
    for %%a in ("*.mp4") do "ffmpeg" -i %%a -ss 00:00:20.000 -c:v copy -c:a copy Trimmed\%%a
    And to cut the end I use 2 bat files:

    Trim1.bat
    Code:
    @echo off
    for %%i in (*.mp4) do (
    call trim2.bat "%%i"
    )
    
    Trim2.bat
    Code:
    @echo off
    for /f "tokens=*" %%a in ('ffprobe -show_format -i %1 ^| find "duration"') do set _duration=%%a
    set _duration=%_duration:~9%
    for /f "delims=. tokens=1*" %%b in ('echo %_duration%') do set /a "_durS=%%b"
    for /f "delims=. tokens=2*" %%c in ('echo %_duration%') do set "_durMS=%%c"
    rem following line is seconds to cut
    set /a "_durS-=20"
    set "_newduration=%_durS%.%_durMS%"
    set "_output=%~n1"
    md _fixed
    ffmpeg -ss 0 -i %1 -t %_newduration% -c copy "Trimmed%_output%.mp4"
    
    
    But this creates more files since it puts the trimmed files into a new folder, then trims those files into another new folder.
    So how do I merge these scripts so it cuts both start and end at the same time?
     
  2. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    103
    10
    #2 Compo, Jun 9, 2015
    Last edited by a moderator: Apr 20, 2017
    Try something like this:
    Code:
    @Echo Off
    SetLocal
    Set "ext=mp4"
    Set "opts=-v quiet"
    Set "opts=%opts% -print_format "compact=print_section=0:nokey=1:escape=csv""
    Set "opts=%opts% -show_entries "format=duration""
    If Exist *.%ext% (If Not Exist "Trimmed\" MD Trimmed)
    For %%a In (*.%ext%) Do Call :Sub "%%~a"
    Exit/B
    
    :Sub
    For /f "Tokens=1* Delims=." %%a In (
    'FFProbe %opts% %1') Do (Set/A "ws=%%a-20" & Set "ps=%%b")
    If %ws% Lss 20 GoTo :EOF
    Set/A hh=ws/(60*60), lo=ws%%(60*60), mm=lo/60, ss=lo%%60
    If %hh% Lss 10 Set hh=0%hh%
    If %mm% Lss 10 Set mm=0%mm%
    If %ss% Lss 10 Set ss=0%ss%
    FFMpeg -i %1 -ss 00:00:20.000 -to %hh%:%mm%:%ss%.%ps:~,3% -c:v copy -c:a copy "Trimmed\Trimmed_%~1"
     
  3. °ツ

    °ツ MDL Addicted

    Jun 8, 2014
    569
    599
    30
    Thank you, it worked well. :cool: