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

MP4 Videos Unnecessarily Encoded During Trim #233

Closed
whargrove opened this issue May 7, 2023 · 3 comments · Fixed by #234
Closed

MP4 Videos Unnecessarily Encoded During Trim #233

whargrove opened this issue May 7, 2023 · 3 comments · Fixed by #234
Labels
bug Something isn't working

Comments

@whargrove
Copy link
Contributor

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
@evamaxfield
Copy link
Member

I don't know if there is a guarantee that the video is an mp4 so how about a fast check of the format and IF it is an mp4, add this codec="copy" otherwise, keep the re-encode to mp4 imo.

Feel free to open a PR!

@whargrove
Copy link
Contributor Author

@evamaxfield I was going to naively use the file extension, but it's a fair point that the ext might be lies!

Using file ext + ffmpeg.probe should be sufficient here.

I'll submit a PR soon with the change.

@evamaxfield
Copy link
Member

@whargrove once this build is done, wait ~5 minutes, then update your instance to use the new release: https://github.com/CouncilDataProject/cdp-backend/actions/runs/4916882908

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants