TransWikia.com

Can I mix copy and encode in one operation to have an accurate and fast cut?

Super User Asked by M.Hamdy.Ghanem on November 3, 2021

I tried many video cutters powered by ffmpeg, and they all have the same issue: Fast custting is not accurate without re-encoding.
I tried many solution in my own app, but nothing works as I need.
So, I have this idea to fix this issue:
suppose I want to generate a new video that doesn’t contain the chuck starts at t1 to t2. I want ffmpeg to do this to divide the segment (t1, t2) to 3 parts:

  1. a part (t1-x, t1+y), which is a complete encoded block that should be re-encoded to be able to copy the part (t1, y) precisely.
  2. a part (t2-z, t3+w), which is a complete encoded block that should be re-encoded to be able to copy the part (z, t2) precisely.
  3. a middle part (y, z) which contains complete encoded blocks, where it can be copied as is.

Then join the 3 parts resulted from the above steps.
Note that the first two parts are expected to be small (and one of them or both can be zero length), so, the re-encoding process will be fast. This will make us able to have exact cuts with slightly slower operation but still super faster than re-encoding the full video. It can be even faster if we can do multiple cuts with one command, so we traverse the frames once.

So, can I do that today with ffmpeg?
Or, can ffmpeg support it soon?
I hope if someone can apply this, and tell us how, or mention some of the ffmpeg team, or deliver it to them anyhow.

One Answer

Ffmpeg supports key-frame cutting using the ss parameter, but its position is all-important.

Example for key-frame seeking:

ffmpeg -ss 30 -i input_vid.mp4 -t 60 -c copy output_clip.mp4

Example for all-frame seeking:

ffmpeg -i input_vid.mp4 -ss 30 -t 60 -c copy output_clip.mp4

The -copy parameter here is important since it causes Stream copy. In some cases, you would need to add the parameter -noaccurate_seek.

For more information see:


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 with the copy mode, ffmpeg has to include all frames before and after the trimmed segment which are needed to decode the segment correctly. This can result in a starting black segment.

To still use copy mode but avoid the black segment, you need to set the switch -avoid_negative_ts.

Answered by harrymc on November 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP