Skip to content

MP4 Videos Unnecessarily Encoded During Trim #233

Description

@whargrove

Describe the Bug

In clip_and_reformat_video if the video is already mp4 ffmpeg will encode the output stream as mp4.

Expected Behavior

Use ffmpeg StreamCopy to avoid re-encoding an already mp4 encoded video.

Reproduction

Proof of concept with ffmpeg-python:

Existing implementation:

import ffmpeg
import os

(
    ffmpeg
    .input('big_buck_bunny_720p_surround.mp4', ss='01:00', to='02:00')
    .output('trimmed.mp4', format='mp4')
    .run()
)

os.remove('trimmed.mp4')

using time python main.py shows real ~3s. I didn't benchmark this so it's anecdotal. On my machine, the encode speed is ~20x (60 / 20 ~= 3).

Use StreamCopy:

import ffmpeg
import os

(
    ffmpeg
    .input('big_buck_bunny_720p_surround.mp4', ss='01:00', to='02:00')
    .output('trimmed.mp4', format='mp4', codec='copy')
    .run()
)

os.remove('trimmed.mp4')

(Note the only difference is codec='copy' arg in output.)

When using StreamCopy, the pipeline on my machine takes ~200ms.

Environment

  • OS Version: Linux ld1 5.19.0-41-generic #42~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 18 17:40:00 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
  • cdp-backend Version: 4.1.0.rc0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions