20

i'm using this command to cut a segment from a video from command line

ffmpeg -i %1 -vcodec copy -acodec copy -ss %2 -t %3 %~n1_cut%~x1

and i call it like

cut.bat video.mp4 001:23:45:678 10

and as expected i get a video_cut.mp4 with 10 seconds of the original video, problem is that few seconds of the resulting file is black screen with audio, why does this happen and how do i fix it?

2
  • 1
    %~n1_cut%~x1 what does this part of the command mean? Commented Aug 6, 2019 at 23:38
  • 1
    @ScipioAfricanus %~n1 expands %1 to a file Name, or if only a path is present (with no trailing backslash) - the last folder in that path and %~x1 expands %1 to a file eXtension only - .txt source:superuser.com/questions/224416/…
    – Shayan
    Commented Sep 30, 2019 at 12:39

2 Answers 2

16

Most videos use codecs which perform temporal compression, so a specified cutpoint may rely on frames before (and after) that cutpoint to be correctly decoded. So, when you use ffmpeg to trim videos and use copy mode, ffmpeg has to include all frames before and after the trimmed segment which are needed to decode the segment correctly.

So, you have two options, use copy mode but set a switch which will allow all players to correctly play these files

ffmpeg -ss %2 -t %3 -i %1 -vcodec copy -acodec copy -avoid_negative_ts make_zero %~n1_cut%~x1

(this will likely result in output that starts before your cutpoint)

or re-encode the streams

ffmpeg -ss %2 -t %3 -i %1 %~n1_cut%~x1
4
  • Although re-encoding is slower, it will produce better results, because with the other solution the video might be longer then what was specified in the -t option.
    – totymedli
    Commented Jul 5, 2018 at 6:48
  • @Gyan What does % do? I tried searching ffmpeg.org/ffmpeg.html but I can't find the usage.
    – Shayan
    Commented May 8, 2019 at 13:34
  • 1
    As your answer suggested I used both -ss and -t before -i and it worked, before, I only put -ss before -i and -t was after it so it didn't work.
    – Shayan
    Commented May 8, 2019 at 13:36
  • So, no way to use -codec copy but not blank video at start of trimmed video? If I have to, I will, but the trimmed video loses quality. The first option starts before cutpoint and that includes copyright music which I want to cut out.
    – Sun
    Commented Feb 8, 2021 at 2:27
12

https://github.com/mifi/lossless-cut/pull/13

When I run ffmpeg with the arguments in this order -i ... -ss ... -t ..., it produces a video file that has no video for a few initial seconds. (as far as I can understand this is because it doesn't seek to a keyframe?)

If I change the order to -ss ... -i ... -t ..., then the resulting cut videos play properly.

7
  • 2
    I tried -ss before -i but it didn't work on my mp4 file.
    – Shayan
    Commented May 8, 2019 at 13:32
  • 2
    Worked for me, thanks. Merely changed the order of parameters. ffmpeg version 3.2.12-1~deb9u1 Commented Aug 4, 2019 at 23:11
  • Actually, this just moves the blank duration to the end of the video
    – Honghao Z
    Commented Dec 5, 2019 at 1:03
  • Changing parameter order worked well for me.
    – Screenack
    Commented Dec 13, 2021 at 1:26
  • It works only partially plus it produces much larger chunks. I thinks is really depending on the codec used. I have a thermal camera footage (5min, 27MB) that I want to split into 30 chunks. With the i-ss-t order I get 30 pieces I get 15MB with most chunks smaller than 600KB but most containing black frames for several seconds (each chunk is 10sec long). With the ss-i-t order I get almost 10MB per chunk with still some seconds of black footage for the first file. Commented Jan 29, 2022 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.