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

true keyframe cut #1984

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

true keyframe cut #1984

wants to merge 2 commits into from

Conversation

ac615223s5
Copy link

@ac615223s5 ac615223s5 commented May 4, 2024

aligns the segments to make keyframe cut more consistent

the segment start will be moved to less than one frame before the first keyframe strictly after the start point, ensuring that the cut will be at the last keyframe at or before the start point.

the endpoint can optionally be aligned to a fraction of a frame before the first keyframe at or after the endpoint. this is useful for splitting a video into segments without repeating or missing any frames.

Summary by CodeRabbit

  • New Features

    • Added a new option for aligning segment times called "True keyframe cut" for more precise editing.
  • Improvements

    • Default alignment for segment times is now set to "True keyframe cut" for better consistency.
  • Enhancements

    • Enhanced keyframe alignment logic to include new modes and improved error handling.

@ac615223s5
Copy link
Author

close #1973
close #330

Copy link

coderabbitai bot commented May 21, 2024

Walkthrough

The recent updates introduce a new option for aligning segment times called "True keyframe cut" across various modules. This involves adding the 'consistent' type to keyframe alignment options, updating default values, and modifying function logic to handle this new alignment type. These changes span the dialogs, ffmpeg utility functions, and segment handling hooks, enhancing the precision of segment alignment in the application.

Changes

File Path Change Summary
src/renderer/src/dialogs/index.tsx Added "True keyframe cut" option in askForAlignSegments function and changed default inputValue to 'consistent'.
src/renderer/src/ffmpeg.ts Introduced consistent type to FindKeyframeMode enum in the findKeyframe function.
src/renderer/src/hooks/useSegments.ts Updated imports to include getStreamFps and getDuration. Modified useSegments function for new keyframe alignment logic.

🐇
In the code where segments meet,
True keyframes now align so neat.
Consistent cuts, precise and clear,
With every frame, no need to fear.
🐰
The rabbit hops with joy so bright,
For code that works just right!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 7e8ac2c and 18ba088.
Files selected for processing (3)
  • src/renderer/src/dialogs/index.tsx (1 hunks)
  • src/renderer/src/ffmpeg.ts (2 hunks)
  • src/renderer/src/hooks/useSegments.ts (2 hunks)
Additional comments not posted (4)
src/renderer/src/dialogs/index.tsx (1)

329-331: The addition of the 'consistent' option in askForAlignSegments aligns well with the PR's goal to improve keyframe cut precision. Changing the default to 'consistent' is a significant change that should be highlighted in user documentation or release notes.

src/renderer/src/ffmpeg.ts (2)

99-99: The expansion of the FindKeyframeMode type to include 'consistent' supports the new feature introduced in this PR. This change should be well-documented to inform developers of the new functionality available.


112-114: The implementation of the 'consistent' mode in findKeyframe correctly focuses on finding the next keyframe, which is crucial for the true keyframe cut feature. Ensure that this behavior is covered by unit tests to prevent regressions in future changes.

src/renderer/src/hooks/useSegments.ts (1)

9-9: Ensure the newly imported functions getStreamFps and getDuration are used appropriately within the file.

@@ -279,18 +279,49 @@ function useSegments({ filePath, workingRef, setWorking, setCutProgress, videoSt
if (response == null) return;
setWorking({ text: i18n.t('Aligning segments to keyframes') });
const { mode, startOrEnd } = response;

if (filePath == null) throw new Error();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly check for filePath being null or undefined at the start of the function to avoid repeated checks and potential runtime errors in deeper nested blocks.

- if (filePath == null) throw new Error();
+ if (!filePath) throw new Error('File path is required.');

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (filePath == null) throw new Error();
if (!filePath) throw new Error('File path is required.');

Comment on lines +282 to +324

if (filePath == null) throw new Error();
const frameTime = 1 / (getStreamFps(videoStream) || 1000);
const duration = await getDuration(filePath);

await modifySelectedSegmentTimes(async (segment) => {
const newSegment = { ...segment };

async function align(key) {
async function align(key: string) {
const time = newSegment[key];
if (filePath == null) throw new Error();
const keyframe = await findKeyframeNearTime({ filePath, streamIndex: videoStream.index, time, mode });
if (keyframe == null) throw new Error(`Cannot find any keyframe within 60 seconds of frame ${time}`);
let keyframe = await findKeyframeNearTime({ filePath, streamIndex: videoStream.index, time, mode });
if (keyframe == null) {
if (mode != 'consistent') {
throw new Error(`Cannot find any keyframe within 60 seconds of frame ${time}`);
}
keyframe = duration;
}
newSegment[key] = keyframe;
}
if (startOrEnd.includes('start')) await align('start');
if (startOrEnd.includes('end')) await align('end');
if (startOrEnd.includes('start')) {
if (mode == 'consistent') {
newSegment.start += frameTime * 0.3;
}
await align('start');
if (mode == 'consistent') {
newSegment.start -= frameTime * 0.7;
}
}
if (startOrEnd.includes('end')) {
await align('end');
if (mode == 'consistent' && newSegment.end != duration) {
newSegment.end -= frameTime * 0.3;
}
}
if (startOrEnd.includes('start')) {
newSegment.start = Math.min(newSegment.start, newSegment.end - frameTime * 0.99); //don't know how ffmpeg interprets cuts between frames
}
else {
newSegment.end = Math.max(newSegment.start + frameTime * 0.99, newSegment.end);
}


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

There are no unit tests covering the new 'consistent' keyframe alignment mode or the useSegments function. This lack of test coverage could lead to untested edge cases and potential issues in production.

  • Ensure that unit tests are added to cover the 'consistent' mode, including edge cases where keyframes might not be found within the expected range.
  • Consider adding tests for the useSegments function to ensure comprehensive coverage.
Analysis chain

The implementation of the new 'consistent' keyframe alignment mode is complex and involves several conditional checks and adjustments to segment times. Ensure thorough testing, especially edge cases where keyframes might not be found within the expected range.

Would you like assistance in writing additional unit tests for this new feature?

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the new 'consistent' mode is covered by unit tests.

# Test: Search for unit tests covering the 'consistent' mode. Expect: Tests that handle various scenarios including edge cases.
rg --type typescript $'describe.*(consistent)'

Length of output: 82



Script:

#!/bin/bash
# Description: Verify the new 'consistent' mode is covered by unit tests.

# Test: Search for unit tests covering the 'consistent' mode. Expect: Tests that handle various scenarios including edge cases.
rg --type ts $'describe.*(consistent)'

Length of output: 39



Script:

#!/bin/bash
# Description: Verify if there are any tests related to the `useSegments` function.

# Test: Search for test cases related to `useSegments`. Expect: Tests that might indirectly cover the 'consistent' mode.
rg --type ts 'useSegments'

Length of output: 880



Script:

#!/bin/bash
# Description: Verify if there are any test files in the repository.

# Test: Search for test files in the repository. Expect: Files that might contain tests for `useSegments`.
fd --type f --extension ts --exec rg 'test|describe|it'

Length of output: 80538

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.

None yet

1 participant