Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lossless trimming to avoid frozen frames at start of videos #9

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

chrominance
Copy link
Collaborator

This PR attempts to fix a few minor problems with nhk-record:

  • nhk-record attempts to cut an MPEG-TS stream at exact timestamps instead of on keyframes, and it does this in a naive fashion. This creates problems with the final file, where the first few frames of a video will be missing the prior keyframe and thus can appear to be "frozen" to the next available keyframe. This doesn't affect playback too much but it is noticeable, and can also affect the beginning of audio playback in extreme circumstances.
  • Sometimes nhk-record also snags the very end of the previous episode (or channel promos) when trimming. This is due to how the boundary detection works: nhk-record looks for silences and black-frame sections of the video stream, and then trims at the start point of a black-frame section. Sometimes ffmpeg will "detect" a black frame that isn't truly black, i.e. when the previous show fades to black.

To sum up the changes:

  • Following the implementation from other programs like Lossless Cut, we take the video stream and detect the keyframes right after the requested start point and right before the requested end point of an episode. Then we re-render the bits before the first keyframe and after the last keyframe, and do a stream copy of everything in between. Finally, we merge the three clips back together into one video.
  • The way the start point of an episode (and end point of an episode) is calculated has changed slightly; now, instead of taking the very beginning of a section of black frames as the cutpoint, we find a point in between (basically 25% of the interval between the beginning and the end of the detected blank section).

I've been running this version of nhk-record for several months now, fixing things along the way, and I think it's ready for wider use. That said, there's a lot going on here so no hard feelings if you'd rather decline this or don't have the time to review!

@CoordSpace
Copy link
Owner

For some reason github didn't send an alert that this pull request was even made! My apologies!

I personally adore Lossless Cut and use it somewhat often for making clips. One quirk with smart cut is the replayability problems that seem to plague the still-ongoing issue - mifi/lossless-cut#126 (Which is recently celebrating being open for 6 years! 🎉)

Have you noticed any weird timing or compatibility issues during your testing?

I'll set up a development instance with your fork and see how it works for a few days, just to be sure the resulting files play well with VLC/MPC/Plex/Jellyfin.

@chrominance
Copy link
Collaborator Author

I haven't tried playing the files with everything you listed but I've tried with VLC, Media Player Classic and Plex via web browser (all on Windows) and all seem to work fine for me. My guess regarding the replayability issues is that Lossless Cut might be having trouble with some specific (potentially exotic) combinations of codec, bitrate, etc. whereas we only have to worry about a few specific combinations, since we know what our video source is going to be.

Hopefully it works as well for you as it does for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants