1

Is there any way to crop the last N seconds from a video? The format is in this case 'MPEG-TS'.

With FFMPEG, I know there is an option for start time and duration, but neither of these are usable in this use case. Video can have any possible length, so the duration cannot be fixed value.

Also, the solution must run in Windows command line and can be automated.

digitalfootmark
  • 577
  • 1
  • 5
  • 19
  • Are your videos limited to a specific container format? If so, look for the container-specific tools such as avisplit from transcode. – George Y. Dec 10 '13 at 23:06
  • Thanks for the hint. The question should have been "**How to crop last N seconds from a TS video with any command line tool in Windows.**" – digitalfootmark Dec 11 '13 at 05:22
  • 1
    Not too many tools working with TS. Try tsMuxeR: http://www.videohelp.com/tools/tsMuxeR – George Y. Dec 12 '13 at 06:57
  • Actually it's also OK if I at first convert them to MP4 h264. And then do the cropping. But tsMuxeR looks very good. I will try that! – digitalfootmark Dec 12 '13 at 09:46
  • ..but the cropping does not support cropping from the end. Only the start and end time for the cropping. :( – digitalfootmark Dec 12 '13 at 09:55

1 Answers1

1

The desired functionality can be achieved with the following steps:

  1. Use ffmpeg to determine the length of the video. Use e.g. a cmd script:

    set input=video.ts
    
    ffmpeg -i "%input%" 2> output.tmp
    
    rem search "  Duration: HH:MM:SS.mm, start: NNNN.NNNN, bitrate: xxxx kb/s"
    for /F "tokens=1,2,3,4,5,6 delims=:., " %%i in (output.tmp) do (
        if "%%i"=="Duration" call :calcLength %%j %%k %%l %%m
    )
    goto :EOF
    
    :calcLength
    set /A s=%3
    set /A s=s+%2*60
    set /A s=s+%1*60*60
    set /A VIDEO_LENGTH_S = s
    set /A VIDEO_LENGTH_MS = s*1000 + %4
    echo Video duration %1:%2:%3.%4 = %VIDEO_LENGTH_MS%ms = %VIDEO_LENGTH_S%s
    
  2. Calculate the start and end point for clip operation

    rem ** 2:00 for the start, 4:00 from the end
    set /A starttime = 120
    set /A stoptime = VIDEO_LENGTH_S - 4*60 
    set /A duration = stoptime - starttime
    
  3. Clip the video. As a bonus, convert the TS file to a more optimal format, such as H.264

    set output=video.mp4
    
    vlc -vvv --start-time %starttime% --stop-time %stoptime% "%input%" 
      --sout=#transcode{vcodec=h264,vb=1200,acodec=mp3, 
      ab=128}:standard{access=file,dst=%output%} vlc://quit
    
    rem ** Handbrake has much better quality **
    handbrakeCLI -i %input% -o %output% -e x264 -B 128 -q 25 
      --start-at duration:%starttime% --stop-at duration:%duration% 
    
digitalfootmark
  • 577
  • 1
  • 5
  • 19