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

refactor: improve ReMux logic with fallback codec combinations #67

Merged
merged 5 commits into from
Jan 4, 2025

Conversation

Tohrusky
Copy link
Member

@Tohrusky Tohrusky commented Jan 4, 2025

Try copy audio and subtitle track first.

Summary by Sourcery

Improve video merging logic by adding fallback codec combinations for audio and subtitles. Try copying both audio and subtitle tracks first, then fall back to FLAC for audio with subtitles, then copying only audio, and finally FLAC for audio only.

Copy link

sourcery-ai bot commented Jan 4, 2025

Reviewer's Guide by Sourcery

This pull request refactors the video merging logic to improve handling of different audio and subtitle track combinations. It introduces a new function ReMuxWithSourceVideo that tries various codec combinations for remuxing, starting with copying both audio and subtitle tracks, then falling back to FLAC for audio with subtitles, copying only audio, and finally FLAC for audio only. The old implementation only tried merging audio and subtitle tracks, and if that failed, it would try merging audio only. The ClearTempFile function is also corrected to handle errors properly.

Sequence diagram for improved ReMux video merging process

sequenceDiagram
    participant MergeVideo
    participant ReMuxWithSourceVideo
    participant FFmpeg
    participant MKVPropedit

    MergeVideo->>FFmpeg: Concat input videos
    FFmpeg-->>MergeVideo: Return concatenated video
    MergeVideo->>ReMuxWithSourceVideo: ReMux with source video
    activate ReMuxWithSourceVideo
    Note over ReMuxWithSourceVideo: Try codec combinations in order:
    Note over ReMuxWithSourceVideo: 1. Copy audio + subtitles
    Note over ReMuxWithSourceVideo: 2. FLAC audio + subtitles
    Note over ReMuxWithSourceVideo: 3. Copy audio only
    Note over ReMuxWithSourceVideo: 4. FLAC audio only
    loop Until success or all combinations tried
        ReMuxWithSourceVideo->>FFmpeg: Try current codec combination
        FFmpeg-->>ReMuxWithSourceVideo: Return result
        alt Success
            ReMuxWithSourceVideo-->>MergeVideo: Return success
        else Failure
            Note over ReMuxWithSourceVideo: Try next combination
        end
    end
    deactivate ReMuxWithSourceVideo
    MergeVideo->>MKVPropedit: Remove tags
    MKVPropedit-->>MergeVideo: Return final video
Loading

State diagram for ReMux codec fallback process

stateDiagram-v2
    [*] --> TryCopyBoth
    TryCopyBoth: Try Copy Audio + Subtitles
    TryFlacBoth: Try FLAC Audio + Subtitles
    TryCopyAudio: Try Copy Audio Only
    TryFlacAudio: Try FLAC Audio Only
    Failed: All Combinations Failed
    Success: ReMux Successful

    TryCopyBoth --> Success: Success
    TryCopyBoth --> TryFlacBoth: Failure

    TryFlacBoth --> Success: Success
    TryFlacBoth --> TryCopyAudio: Failure

    TryCopyAudio --> Success: Success
    TryCopyAudio --> TryFlacAudio: Failure

    TryFlacAudio --> Success: Success
    TryFlacAudio --> Failed: Failure

    Success --> [*]
    Failed --> [*]
Loading

File-Level Changes

Change Details Files
Implement fallback codec combinations for video merging
  • Introduce a new function ReMuxWithSourceVideo to handle remuxing with different codec combinations.
  • Replace the old merging logic with a call to ReMuxWithSourceVideo.
  • Implement fallback logic within ReMuxWithSourceVideo to try different codec combinations for audio and subtitles.
module/ffmpeg/merge.go
Correct the name of the utility function ClearTempFile
  • Rename ClaerTempFile to ClearTempFile.
module/ffmpeg/merge.go
module/ffmpeg/cut.go
module/ffmpeg/vapoursynth.go
module/util/file.go
worker/internal/cut/worker.go
worker/internal/encode/worker.go
worker/internal/merge/worker.go
Rename originFile to originPath in MergeVideo function signature
  • Change the parameter name from originFile to originPath.
module/ffmpeg/merge.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Tohrusky - I've reviewed your changes - here's some feedback:

Overall Comments:

  • There appears to be a typo in the function name 'ClaerTempFile' - should be 'ClearTempFile'
  • Consider making the commit message more descriptive of the actual changes, e.g., 'refactor: implement fallback codec combinations for more robust video remuxing'
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -108,3 +71,46 @@ func MergeVideo(originFile string, inputFiles []string, outputPath string) error
log.Logger.Infof("Remove tags output: %s", out)
return nil
}

// ReMuxWithSourceVideo 使用 ffmpeg 和原始视频进行 remux
func ReMuxWithSourceVideo(originPath string, outputPath string, concatOutputPath string) error {
Copy link

Choose a reason for hiding this comment

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

issue: Missing subtitle stream mapping in some codec combinations

When using codec combinations that include '-c:s copy', you should also include '-map 0:s' to ensure subtitles are properly mapped from the source video. Without this, subtitles might be lost even when attempting to copy them.

@@ -10,7 +10,7 @@
)

// MergeVideo 使用 ffmpeg 进行视频合并,使用 mkvpropedit 清除 tags
func MergeVideo(originFile string, inputFiles []string, outputPath string) error {
func MergeVideo(originPath string, inputFiles []string, outputPath string) error {
listPath := "temp_list.txt" // 写入文件列表
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Use of hardcoded temporary file paths could cause conflicts in concurrent operations

Consider generating unique temporary file paths using ioutil.TempFile or similar to avoid potential conflicts when multiple instances run simultaneously.

@@ -108,3 +71,46 @@
log.Logger.Infof("Remove tags output: %s", out)
return nil
}
Copy link

Choose a reason for hiding this comment

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

issue (typo): Typo in function name 'ClaerTempFile'

Suggested implementation:

		_ = util.ClearTempFile(outputPath)

Note: You'll also need to ensure that the function is actually named ClearTempFile in the util package. If the function definition also has the typo, you'll need to fix it there as well in the util package.

@Tohrusky
Copy link
Member Author

Tohrusky commented Jan 4, 2025

@sourcery-ai review

@Tohrusky
Copy link
Member Author

Tohrusky commented Jan 4, 2025

@sourcery-ai summary

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Tohrusky - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

_ = util.ClearTempFile(outputPath)
}

return fmt.Errorf("all codec combinations failed")
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider collecting and including individual codec errors in the final error message for better debugging

This would help diagnose why all attempts failed instead of just indicating that they did.

Suggested implementation:

		// Log the error and try the next combination
		log.Logger.Errorf("Merge failed with codec combination %v: %v", codecArgs, err)
		// If failed, clean up temp files which may have been created
		_ = util.ClearTempFile(outputPath)
		mergeErrors = append(mergeErrors, fmt.Sprintf("codec %v: %v", codecArgs, err))
	}

	return fmt.Errorf("all codec combinations failed:\n%s", strings.Join(mergeErrors, "\n"))
func ReMuxWithSourceVideo(originPath string, outputPath string, concatOutputPath string) error {
	// Collect errors from each attempt
	var mergeErrors []string

	// Define the different codec combinations to try

You'll need to add "strings" to the imports if it's not already imported:

import (
    "strings"
    // ... other imports
)

@Tohrusky
Copy link
Member Author

Tohrusky commented Jan 4, 2025

@sourcery-ai review

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Tohrusky - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Tohrusky Tohrusky merged commit 2055dcc into TensoRaws:main Jan 4, 2025
2 checks passed
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.

1 participant