mifi / lossless-cut

The swiss army knife of lossless video/audio editing
https://losslesscut.app/
GNU General Public License v2.0
25.98k stars 1.25k forks source link

Drop all non-keyframes to speed up long footage #2111

Open TuxRobotics opened 1 month ago

TuxRobotics commented 1 month ago

The fewer issues I have to read, the more new features I will have time to implement, so I ask that you please try these things first

Description

In the "Export Mode" dropdown, it would be awesome if there were a "Fast Forward Cuts" option. Instead of cutting, it would speed up the frame rate by an integer multiple and merge.

It would be fantastic for quickly cropping down long security camera footage!

To keep export performance, it could:

  1. Be restricted to integer frame rate multiples
  2. Only export keyframes in the "cut" section
  3. "FF" segments would Essentially just be a few more params passed to FFMPEG

Thanks, awesome work!

mifi commented 1 month ago

I think you can already do that by clicking on the change speed icon next to the fps in the lower left

TuxRobotics commented 1 month ago

That is close but it still outputs all the frames and just tells the player to speed up. This means big files and most hardware can't handle high frame rates. Also, it can't be applied to cut sections.

mifi commented 1 month ago

It’s not possible to losslessly change the speed without re-encoding or playing existing frames faster afaik, or am i missing something?

TuxRobotics commented 1 month ago

FFMPEG can actually do this by grabbing only the keyframes and copying them, at least for H264.

I tried this quickly on a video and it worked (no transcoding involved).

ffmpeg -discard nokey -i t1.mp4 -c copy t1.264 ffmpeg -r 30 -i t1.264 -c copy t1keys.mp4

Source: https://video.stackexchange.com/questions/21970/speed-up-a-video-30x-using-only-key-frames-with-ffmpeg

mifi commented 4 weeks ago

Ohh okay cool! Gyan from the ffmpeg team always seems to have the answers πŸ™Œ

But then you are at the mercy of the keyframe interval, right? it wouldn't allow "speed up the frame rate by an integer multiple". The resulting file would play only the keyframes and nothing else. That would effectively give a keyframe interval of 1. I think some players have problems playing back videos that only have keyframes (as it requires more bandwidth and cpu to decode keyframes compared to other frames).

I agree it would be a cool trick, maybe we can have a tool in the "Tools" menu that allows stripping out non-keyframes from a video using this command.

also: even though technically the keyframes don't lose any quality, in a whole it's quite a lossy operation because all non-keyframes are lost πŸ˜… but I guess the main benefit is that the operation would be very fast.

TuxRobotics commented 3 weeks ago

Yes, I agree it is lossy, but it is fast and less lossy than "cut" right? πŸ˜…

For playback, I had made the assumption that it would take less CPU power to play keyframe-only sections, but you are right that the memory/file bandwidth probably jumps a lot.

Instead of the tools menu, if a user could click on the "trash" icon in the timeline and have it change to a ">>" or similar for the segment it would be slick. Might be difficult to differentiate the icon click from a seek click?

I am poking around the FFMPEG source to see if there is a way to throw out some of the keyframes as well to be able to adjust the speedup

TuxRobotics commented 3 weeks ago

Yeah, doesn't look like there is a way to do any faster than keyframes only. Still pretty useful level of speed up though. I will look at submitting a pull request to FFMPEG for skipping a variable number of keyframes as well. A surprising number of codecs work with this.

mifi commented 2 weeks ago

For playback, I had made the assumption that it would take less CPU power to play keyframe-only sections, but you are right that the memory/file bandwidth probably jumps a lot.

I just assumed that rendering a high quality detailed keyframe takes more cpu than calculating the next frame based on an existing frame and a motion vector, but i might be wrong.

Instead of the tools menu, if a user could click on the "trash" icon in the timeline and have it change to a ">>" or similar for the segment it would be slick. Might be difficult to differentiate the icon click from a seek click?

That would not be trivial to implement so I probably won't implement it that way for such a niche use case unless it becomes a tremendously popular request.

I will look at submitting a pull request to FFMPEG for skipping a variable number of keyframes as well. A surprising number of codecs work with this. cool