Never re-encode damaged originals; check the copy's frames against the original #12

Merged
kompas-review merged 1 commit from khyretos/fix/never-keep-cut-off-copies into main 2026-11-22 02:57:40 +01:00 AGit
Owner

A copy shorter than its original was kept since PR 8 with a "cut off" note.
Those originals were partial downloads (video stopping before the audio, or
holes with no video), the copy had the same damage and played out of sync,
and Finalize deleted the only original. Now the original's video packets are
read before encoding: holes over 2 s or video ending over 2 s early skip the
file and leave it as it is. The copy must have the original's frame count,
end and holes, or it fails (after one CPU-decode try for GPU encodes).

Co-Authored-By: Claude Opus 5.5 (1M context) noreply@anthropic.com

A copy shorter than its original was kept since PR 8 with a "cut off" note. Those originals were partial downloads (video stopping before the audio, or holes with no video), the copy had the same damage and played out of sync, and Finalize deleted the only original. Now the original's video packets are read before encoding: holes over 2 s or video ending over 2 s early skip the file and leave it as it is. The copy must have the original's frame count, end and holes, or it fails (after one CPU-decode try for GPU encodes). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Never re-encode damaged originals; check the copy's frames against the original
All checks were successful
kreative-pr/review reviewed: 0 finding(s)
kk / all-checks All 1 checks passed
8f7284516f
A copy shorter than its original was kept since PR 8 with a "cut off" note.
Those originals were partial downloads (video stopping before the audio, or
holes with no video), the copy had the same damage and played out of sync,
and Finalize deleted the only original. Now the original's video packets are
read before encoding: holes over 2 s or video ending over 2 s early skip the
file and leave it as it is. The copy must have the original's frame count,
end and holes, or it fails (after one CPU-decode try for GPU encodes).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Collaborator

Title

Never re-encode damaged originals; check the copy's frames against the original


PR Type

Bug fix, Tests


Description

  • Prevents re-encoding damaged originals with missing video or holes.

  • Validates AV1 copies against original frame counts and timing.

  • Skips files where video ends early or contains gaps longer than 2 seconds.


Diagram Walkthrough

flowchart TD
  Start["Start Encoding"] --> CheckOriginal["Check Original Timeline"]
  CheckOriginal -.->|"No damage"| Stage["Stage for Encoding"]
  CheckOriginal -.->|"Damaged (holes/short)"| Skip["Skip & Re-download"]
  Stage --> EncodeGPU["Try GPU Encode"]
  EncodeGPU -.->|"Success & OK"| Finalize["Finalize"]
  EncodeGPU -.->|"Failed/Short"| TryCPU["Fallback CPU Decode"]
  TryCPU --> ValidateCopy["Validate Copy Frames"]
  ValidateCopy -.->|"OK"| Finalize
  ValidateCopy -.->|"Lost Video"| Fail["Fail & Remove"]

File Walkthrough

Relevant files
Configuration changes
rx_config.py
Increment version number to 1.0.1                                               

app/rx_config.py

  • Updated version string from 1.0.0 to 1.0.1.
  • No functional logic changes in this file.
+1/-1     
Enhancement
rx_media.py
Add timeline analysis for damaged video detection               

app/rx_media.py

  • Replaced video_packets_after with video_timeline to extract packet
    data without decoding.
  • Added GAP_S constant to define hole duration threshold of 2 seconds.
  • New functions _damage and _lost_video analyze timeline data for
    errors.
+28/-13 
Bug fix
rx_worker.py
Integrate damage checks into worker processing flow           

app/rx_worker.py

  • Calls video_timeline before encoding to detect damaged originals.
  • Skips files with holes or truncated video instead of re-encoding them.
  • Validates encoded copies against original frame counts and end times.
+56/-11 
Tests
test_damaged_sources.py
Update tests for new timeline and damage detection             

tests/test_damaged_sources.py

  • Renamed test_video_packets_after to test_video_timeline.
  • Added tests for detecting holes, truncated video, and frame loss.
  • Updated mock expectations to use new timeline structure.
+57/-11 
test_fixes_2026_11_07.py
Align fallback tests with new timeline API                             

tests/test_fixes_2026_11_07.py

  • Updated mock patch to use video_timeline instead of
    video_packets_after.
  • Adjusted test data to match new timeline dictionary format.
+2/-1     
Documentation
CHANGELOG.md
Document version 1.0.1 release notes                                         

CHANGELOG.md

  • Added entry for version 1.0.1 detailing the damage prevention fix.
  • Explained previous behavior of keeping cut-off copies.
+5/-0     

## Title <!-- pr-agent:describe:persistent --> Never re-encode damaged originals; check the copy's frames against the original ___ ### **PR Type** Bug fix, Tests ___ ### **Description** - Prevents re-encoding damaged originals with missing video or holes. - Validates AV1 copies against original frame counts and timing. - Skips files where video ends early or contains gaps longer than 2 seconds. ___ ### Diagram Walkthrough ```mermaid flowchart TD Start["Start Encoding"] --> CheckOriginal["Check Original Timeline"] CheckOriginal -.->|"No damage"| Stage["Stage for Encoding"] CheckOriginal -.->|"Damaged (holes/short)"| Skip["Skip & Re-download"] Stage --> EncodeGPU["Try GPU Encode"] EncodeGPU -.->|"Success & OK"| Finalize["Finalize"] EncodeGPU -.->|"Failed/Short"| TryCPU["Fallback CPU Decode"] TryCPU --> ValidateCopy["Validate Copy Frames"] ValidateCopy -.->|"OK"| Finalize ValidateCopy -.->|"Lost Video"| Fail["Fail & Remove"] ``` <details> <summary><h3> File Walkthrough</h3></summary> <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Configuration changes</strong></td><td><table> <tr> <td> <details> <summary><strong>rx_config.py</strong><dd><code>Increment version number to 1.0.1</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> app/rx_config.py <ul><li>Updated version string from 1.0.0 to 1.0.1.<br> <li> No functional logic changes in this file.</ul> </details> </td> <td><a href="https://git.kreative-kompas.com/khyretos/kk-reencoder/src/branch/refs/pull/12/head/app/rx_config.py">+1/-1</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></td></tr><tr><td><strong>Enhancement</strong></td><td><table> <tr> <td> <details> <summary><strong>rx_media.py</strong><dd><code>Add timeline analysis for damaged video detection</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> app/rx_media.py <ul><li>Replaced <code>video_packets_after</code> with <code>video_timeline</code> to extract packet <br>data without decoding.<br> <li> Added <code>GAP_S</code> constant to define hole duration threshold of 2 seconds.<br> <li> New functions <code>_damage</code> and <code>_lost_video</code> analyze timeline data for <br>errors.</ul> </details> </td> <td><a href="https://git.kreative-kompas.com/khyretos/kk-reencoder/src/branch/refs/pull/12/head/app/rx_media.py">+28/-13</a>&nbsp; </td> </tr> </table></td></tr><tr><td><strong>Bug fix</strong></td><td><table> <tr> <td> <details> <summary><strong>rx_worker.py</strong><dd><code>Integrate damage checks into worker processing flow</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> app/rx_worker.py <ul><li>Calls <code>video_timeline</code> before encoding to detect damaged originals.<br> <li> Skips files with holes or truncated video instead of re-encoding them.<br> <li> Validates encoded copies against original frame counts and end times.</ul> </details> </td> <td><a href="https://git.kreative-kompas.com/khyretos/kk-reencoder/src/branch/refs/pull/12/head/app/rx_worker.py">+56/-11</a>&nbsp; </td> </tr> </table></td></tr><tr><td><strong>Tests</strong></td><td><table> <tr> <td> <details> <summary><strong>test_damaged_sources.py</strong><dd><code>Update tests for new timeline and damage detection</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> tests/test_damaged_sources.py <ul><li>Renamed <code>test_video_packets_after</code> to <code>test_video_timeline</code>.<br> <li> Added tests for detecting holes, truncated video, and frame loss.<br> <li> Updated mock expectations to use new timeline structure.</ul> </details> </td> <td><a href="https://git.kreative-kompas.com/khyretos/kk-reencoder/src/branch/refs/pull/12/head/tests/test_damaged_sources.py">+57/-11</a>&nbsp; </td> </tr> <tr> <td> <details> <summary><strong>test_fixes_2026_11_07.py</strong><dd><code>Align fallback tests with new timeline API</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> tests/test_fixes_2026_11_07.py <ul><li>Updated mock patch to use <code>video_timeline</code> instead of <br><code>video_packets_after</code>.<br> <li> Adjusted test data to match new timeline dictionary format.</ul> </details> </td> <td><a href="https://git.kreative-kompas.com/khyretos/kk-reencoder/src/branch/refs/pull/12/head/tests/test_fixes_2026_11_07.py">+2/-1</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></td></tr><tr><td><strong>Documentation</strong></td><td><table> <tr> <td> <details> <summary><strong>CHANGELOG.md</strong><dd><code>Document version 1.0.1 release notes</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> CHANGELOG.md <ul><li>Added entry for version 1.0.1 detailing the damage prevention fix.<br> <li> Explained previous behavior of keeping cut-off copies.</ul> </details> </td> <td><a href="https://git.kreative-kompas.com/khyretos/kk-reencoder/src/branch/refs/pull/12/head/CHANGELOG.md">+5/-0</a>&nbsp; &nbsp; &nbsp; </td> </tr> </table></td></tr></tr></tbody></table> </details> ___
Collaborator

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🔒 No security concerns identified
⚡ No major issues detected
## PR Reviewer Guide 🔍 <!-- pr-agent:review:full --> Here are some key observations to aid the review process: <table> <tr><td>🔒&nbsp;<strong>No security concerns identified</strong></td></tr> <tr><td>⚡&nbsp;<strong>No major issues detected</strong></td></tr> </table>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
khyretos/kk-reencoder!12
No description provided.