# How the Per-Segment Extraction and Lossless Concat Pipeline Avoids Double Encoding

> Learn how the per-segment extraction and lossless concat pipeline avoids double encoding. Discover how each video segment is encoded once and then concatenated losslessly using ffmpeg copy to preserve quality.

- Repository: [Browser Use/video-use](https://github.com/browser-use/video-use)
- Tags: internals
- Published: 2026-07-05

---

**The per-segment extraction and lossless concat pipeline prevents double encoding by encoding each video segment exactly once during the extraction phase, then concatenating the resulting files using ffmpeg’s `-c copy` flag to stream-copy bitstreams without re-initializing the codec.**

The **per-segment extraction and lossless concat pipeline** implemented in the **browser-use/video-use** repository guarantees single-pass encoding by separating video processing into two distinct stages. By handling all visual grading, HDR tone-mapping, and audio fades during the initial extraction phase, the pipeline ensures that the final assembly step merely copies existing encoded data rather than re-compressing it.

## Per-Segment Extraction: Single-Pass Encoding

The pipeline encodes each segment exactly once using the `extract_segment` function defined in [`helpers/render.py`](https://github.com/browser-use/video-use/blob/main/helpers/render.py) (lines 61-106). This function receives a single cut from the Edit Decision List (EDL) and executes an ffmpeg command that seeks to the precise start time using `-ss`, limits the duration with `-t`, and applies the complete filter chain—including visual grades, optional HDR tone-mapping, scaling, and 30 ms audio fades.

Because this step encodes the segment into a self-contained MP4 using **libx264** at a fast **CRF 20** for final renders, all processing that affects pixel or audio data is baked into the file immediately. Subsequent stages treat this output as a finished artifact, ensuring that the demanding encoding operation happens only once per frame.

### Precise Timing and Filter Application

During extraction, ffmpeg handles the heavy lifting of seeking and filtering:
- **Visual grading** and **HDR tone-mapping** are applied before the encoder receives the frame.
- **Audio fades** are rendered into the PCM stream during this single pass.
- The resulting segment is written to a temporary MP4 file in the edit directory, preserving exact timestamps and synchronization.

## Lossless Concatenation: Stream-Copy Assembly

After all segments are extracted, the `concat_segments` function in [`helpers/render.py`](https://github.com/browser-use/video-use/blob/main/helpers/render.py) (lines 66-84) handles the final assembly. This function generates a temporary text file listing all segment paths in playback order, then invokes ffmpeg’s **concat demuxer** with the `-c copy` option.

The `-c copy` directive instructs ffmpeg to perform a **stream copy**—copying the video and audio packets directly from the input files to the output container without decoding or re-encoding. The concat demuxer simply rewrites container headers to stitch the bitstreams together, preserving the exact byte-for-byte codec data from each segment.

### The Concat Demuxer and `-c copy`

Using the concat demuxer with `-c copy` ensures:
- **No quality loss** from successive re-encodes.
- **Fast processing** because ffmpeg skips the computationally expensive encode/decode cycle.
- **Consistent timestamps** across segment boundaries since the original presentation timestamps are preserved.

## Complete Implementation Example

To render an EDL without double encoding, the pipeline orchestrates the extraction and concatenation functions sequentially:

```python
from pathlib import Path
import json
from helpers.render import extract_all_segments, concat_segments

edl_path = Path("my_edit.edl.json")
edit_dir = Path("my_edit")          # temp folder for intermediate files

out_path = Path("final.mp4")

# 1️⃣ Extract every segment (grade, HDR tone-mapping, 30 ms fades)

segment_paths = extract_all_segments(
    edl=json.loads(edl_path.read_text()),
    edit_dir=edit_dir,
    preview=False,      # final quality

    draft=False,
)

# 2️⃣ Concatenate the already-encoded segments losslessly

concat_segments(segment_paths, out_path, edit_dir)

```

For command-line usage, the repository provides a direct entry point:

```bash
python helpers/render.py my_edit.edl.json -o final.mp4

```

This script internally executes `extract_all_segments` → `extract_segment` → `concat_segments`, guaranteeing that video data traverses the encoder exactly once.

## Summary

- **Single-pass encoding** occurs in `extract_segment` ([`helpers/render.py`](https://github.com/browser-use/video-use/blob/main/helpers/render.py), lines 61-106), where ffmpeg applies grades, tone-mapping, and fades using libx264 at CRF 20.
- **Lossless assembly** is performed by `concat_segments` ([`helpers/render.py`](https://github.com/browser-use/video-use/blob/main/helpers/render.py), lines 66-84) using the concat demuxer with `-c copy` to stream-copy bitstreams.
- The separation of concerns ensures **no generational loss**, **fast concatenation**, and **consistent audio-visual synchronization** across cut boundaries.

## Frequently Asked Questions

### Why is double encoding harmful to video quality?

Double encoding—re-compressing already compressed video—introduces **generational loss** and compression artifacts. Each pass through a lossy codec like H.264 discards additional data to achieve target bitrates, resulting in diminished detail, color banding, and macroblocking. The per-segment extraction pipeline avoids this by ensuring pixels are encoded only once before final assembly.

### What does the `-c copy` flag do in ffmpeg?

The `-c copy` flag instructs ffmpeg to **stream-copy** the audio and video data without decoding or re-encoding. Instead of processing raw frames, ffmpeg copies the compressed packets directly from the input container to the output container. This preserves the exact bitstream quality while drastically reducing processing time.

### How does the pipeline handle audio fades across segment boundaries?

The 30 ms audio fades are **baked in during the extraction phase** by the `extract_segment` function. Since fades are applied as filters during the single encoding pass, the resulting segment files already contain the smoothed audio transitions. The lossless concatenation step then joins these pre-faded segments without altering the audio stream, maintaining seamless boundaries in the final output.

### Where is the encoding quality configured in the source code?

Encoding parameters are defined within the `extract_segment` function in [`helpers/render.py`](https://github.com/browser-use/video-use/blob/main/helpers/render.py). The pipeline uses **libx264** with a **CRF 20** setting for final renders, balancing quality and file size. This configuration ensures that the single encoding pass produces broadcast-ready output suitable for the lossless concatenation stage.