Skip to content

TSI-3418: fix(worker): 裸 -c copy 被静默改写为重新编码(HEVC→H264 有损转换) - #78

Merged
tsic404 merged 1 commit into
mainfrom
multica/TSI-3418
Sep 21, 2026
Merged

tsic404 merged 1 commit into
mainfrom
multica/TSI-3418

Conversation

@multica-tsic404

@multica-tsic404 multica-tsic404 Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Closes TSI-3418

Summary by Sourcery

Prevent rewrite decisions from replacing FFmpeg stream-copy commands with unintended video re-encoding.

Bug Fixes:

  • Preserve bare general codec copy directives without silently converting them into lossy video re-encodes.
  • Correctly recognize supported explicit video codec flag forms while allowing video-specific settings to override general copy directives.

Tests:

  • Add regression coverage for bare and inline copy syntax, explicit video codec precedence, non-video codec flags, and verbatim argument passthrough.

@sourcery-ai

sourcery-ai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Fixes silent lossy re-encoding of commands using bare -c/-codec copy by recognizing general stream-copy directives while preserving video-specific overrides, and adds parser plus end-to-end regression tests.

Flow diagram for FFmpeg encoder argument classification

flowchart TD
    A[FFmpeg arguments] --> B{Video-specific codec flag present?}
    B -->|Yes| C[parseEncoderFromArgs]
    B -->|No| D{General codec flag sets copy?}
    D -->|Yes| E[Return encoder.EncoderFamily copy]
    D -->|No| F[Return empty encoder family]
    C --> G[Use video-specific encoder override]
    E --> H[Preserve stream copy]
    G --> I[Rewrite engine continues with classified encoder]
    H --> I
Loading

File-Level Changes

Change Details Files
Preserve bare general stream-copy directives instead of treating them as missing encoder selection and rewriting them.
  • Detect -c copy, -codec copy, and inline -c=copy/-codec=copy.
  • Prioritize video-specific codec flags over a general copy directive.
  • Leave non-copy general codec values to the existing auto-selection path.
pkg/worker/rewrite_adapter.go
Add focused unit and end-to-end regression coverage for stream-copy handling.
  • Cover separated and inline general copy flag forms, precedence, non-copy codecs, and audio-only codec flags.
  • Verify bare copy arguments pass through verbatim with no rewrite or resolved target encoder, including mixed audio re-encoding.
pkg/worker/rewrite_adapter_test.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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="pkg/worker/rewrite_adapter.go" line_range="388-398" />
<code_context>
 		}
 	}
+
+	// General codec flag: bare "-c copy" sets every stream (video included)
+	// to stream-copy. Treat it as the "copy" passthrough encoder so the
+	// rewrite engine never injects a lossy re-encode.
+	for i, arg := range args {
+		if arg == "-c" || arg == "-codec" {
+			if i+1 < len(args) && args[i+1] == "copy" {
+				return encoder.EncoderFamily("copy")
+			}
+		}
+		if arg == "-c=copy" || arg == "-codec=copy" {
+			return encoder.EncoderFamily("copy")
+		}
+	}
</code_context>
<issue_to_address>
**issue (bug_risk):** When arguments contain an inline stream-specific option such as `-codec:v=h264_qsv` or `-vcodec=h264_qsv` together with `-c copy`, the first scan does not recognize that stream-specific form and the new general-option scan returns `copy`. The adapter therefore skips rewriting and leaves the explicitly requested video encoder unchanged, even when it is unavailable and should be handled by the rewrite or fallback path.

**Triggers:** When FFmpeg arguments combine general `-c copy` with inline `-codec:v=...` or `-vcodec=...`.

**Suggested fix:** Recognize `-codec:v=<encoder>` and `-vcodec=<encoder>` in the stream-specific scan before considering general codec flags.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and if the parser misclassifies a command, the worker could leave an output video in an unsupported codec instead of re-encoding it, or fail to apply a needed conversion. That incorrect media artifact outlives a revert, but it is bounded and can be regenerated after reverting the change.

Blocking findings: pkg/worker/rewrite_adapter.go:398


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread pkg/worker/rewrite_adapter.go

@multica-tsic404 multica-tsic404 Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

审查规模:L1(93 行 / 2 文件),Bug 修复。修复方向正确:裸 -c copy 短路为 passthrough,端到端回归测试覆盖透传与 ResolveTargetEncoder 置空,点赞。

通过维度:代码质量 ✓ 安全 ✓ 架构 ✓ 规范 ✓ 性能 ✓ 并发 ✓ API 兼容 ✓。安全:纯参数解析,无新增依赖、无 shell 拼接、无敏感信息(显式检查)。

已纳入 sourcery-ai 审查反馈(COMMENTED,非 CHANGES_REQUESTED):其指出的内联 -codec:v=/-vcodec= 与 -c copy 组合误判已核实为真实缺陷,见内联评论 1;另见评论 2(注释语义与位置优先级)。

Comment thread pkg/worker/rewrite_adapter.go
Comment thread pkg/worker/rewrite_adapter.go Outdated

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

@multica-tsic404 multica-tsic404 Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

复审提交 5eb51c3:2 个阻塞问题均已解决。

  1. 🔴 pkg/worker/rewrite_adapter.go(原 :392,现 :384-390)— 修复正确:首个扫描现通过 strings.HasPrefix 识别 -c:v= / -codec:v= / -vcodec= 三种内联形式(切片偏移 5/9/8 逐一核对正确),显式视频编码器优先于通用 -c copy,不再误判为纯 copy 透传。
  2. 🟡 pkg/worker/rewrite_adapter.go:366-372 注释 — 现准确表述:显式视频标志优先、且注明 ffmpeg 位置语义(later wins)与本解析器的取舍,不再绝对化。

测试补齐:rewrite_adapter_test.go 新增内联形式与「内联 -codec:v=/-vcodec= 覆盖通用 -c copy」用例,恰好覆盖 Sourcery 原始 bug_risk 场景;端到端回归测试(透传 + ResolveTargetEncoder 置空)保留。

安全维度已显式检查:仍为纯参数解析,无新增依赖(go.mod 未动)、无 shell 拼接、无敏感信息输出。并发/性能/API 兼容同前轮:parseEncoderFromArgs 无共享状态、O(n) 单趟、公共接口未变。

sourcery-ai 已同步转 APPROVED。零发现。

@tsic404
tsic404 merged commit 4778df5 into main Sep 21, 2026
2 checks passed
@tsic404
tsic404 deleted the multica/TSI-3418 branch September 21, 2026 14:02
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