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

FFmpeg: Improve performance by seeking input rather than output video #3893

Merged

Conversation

alexspurling
Copy link
Contributor

This improves performance when converting video to images as we no longer have to decode and then discard the first x seconds of video.

The current code creates an ffmpeg command like this:

/usr/bin/ffmpeg -y -i /go/src/github.com/photoprism/photoprism/storage/originals/2023/01/20230130_101623_A472124D.mp4 -ss 00:00:30.000 -vframes 1 /go/src/github.com/photoprism/photoprism/storage/sidecar/2023/01/20230130_101623_A472124D.mp4.jpg

This tells ffmpeg to decode the first 30s of the input before extracting a frame and writing it as a jpg.

This pull request changes the ffmpeg command to:

/usr/bin/ffmpeg -y -ss 00:00:30.000 -i /go/src/github.com/photoprism/photoprism/storage/originals/2023/01/20230130_101623_A472124D.mp4 -vframes 1 /go/src/github.com/photoprism/photoprism/storage/sidecar/2023/01/20230130_101623_A472124D.mp4.jpg

This tells ffmpeg to efficiently seek to the desired frame, write the jpg and then exit. This results in a significant speed up when importing large video files.

Log file before:

2023-11-19 10:15:27 INFO upload has been processed
2023-11-19 10:15:27 INFO upload: imported 2 files
2023-11-19 10:15:27 INFO upload: deleted empty folder /go/src/github.com/photoprism/photoprism/storage/users/us4d78b290vwb8x0/upload/sessk1toutkldiu6jt
2023-11-19 10:15:27 WARN import: 2023/01/20230130_101623_A472124D.mp4 exceeds file size limit (2.8 GB / 1.0 GB)
2023-11-19 10:15:27 INFO media: created 11 thumbnails for 2023/01/20230130_101623_A472124D.mp4.jpg [428.211754ms]
2023-11-19 10:15:27 INFO convert: 20230130_101623_A472124D.mp4.jpg created in 9.770663681s (ffmpeg)
2023-11-19 10:15:17 INFO convert: converting 20230130_101623_A472124D.mp4 to 20230130_101623_A472124D.mp4.jpg (ffmpeg)
2023-11-19 10:15:09 INFO import: moving main mp4 file P1870007.MP4 to 2023/01/20230130_101623_A472124D.mp4
2023-11-19 10:15:02 INFO media: P1870007.MP4 was taken at 2023-01-30 10:16:23.337 +0000 UTC (meta)

Log file after:

2023-11-19 10:32:39 INFO upload has been processed
2023-11-19 10:32:39 INFO upload: imported 2 files
2023-11-19 10:32:39 INFO upload: deleted empty folder /go/src/github.com/photoprism/photoprism/storage/users/us4d78b290vwb8x0/upload/sessk1toutkliytixjf
2023-11-19 10:32:39 WARN import: 2023/01/20230130_101623_A472124D.mp4 exceeds file size limit (2.8 GB / 1.0 GB)
2023-11-19 10:32:39 INFO media: created 11 thumbnails for 2023/01/20230130_101623_A472124D.mp4.jpg [461.212762ms]
2023-11-19 10:32:38 INFO convert: 20230130_101623_A472124D.mp4.jpg created in 707.282911ms (ffmpeg)
2023-11-19 10:32:38 INFO convert: converting 20230130_101623_A472124D.mp4 to 20230130_101623_A472124D.mp4.jpg (ffmpeg)
2023-11-19 10:32:30 INFO import: moving main mp4 file P1870007.MP4 to 2023/01/20230130_101623_A472124D.mp4
2023-11-19 10:32:24 INFO media: P1870007.MP4 was taken at 2023-01-30 10:16:23.337 +0000 UTC (meta)

Acceptance Criteria:

  • Features and enhancements must be fully implemented so that they can be released at any time without additional work
  • Automated unit and/or acceptance tests are mandatory to ensure the changes work as expected and to reduce repetitive manual work
  • Frontend components must be responsive to work and look properly on phones, tablets, and desktop computers; you must have tested them on all major browsers and different devices
  • Documentation and translation updates should be provided if needed
  • In case you submit database-related changes, they must be tested and compatible with SQLite 3 and MariaDB 10.5.12+

This improves performance when converting video to images as we no longer
have to decode and then discard the first x seconds of video.
@CLAassistant
Copy link

CLAassistant commented Nov 19, 2023

CLA assistant check
All committers have signed the CLA.

@lastzero
Copy link
Member

Excellent! I wasn't aware that changing the parameter order helps in this case.

@lastzero lastzero self-requested a review November 19, 2023 14:54
@lastzero lastzero added enhancement Refactoring, improvement or maintenance task performance Performance Optimization video Video Formats, Transcoding, FFmpeg, Streaming & Co merged Changes should be tested again after they have been integrated labels Nov 19, 2023
@lastzero lastzero merged commit d32bc4c into photoprism:develop Nov 19, 2023
2 checks passed
@lastzero lastzero added the please-test Ready for acceptance test label Nov 19, 2023
@lastzero
Copy link
Member

lastzero commented Nov 19, 2023

@lastzero lastzero changed the title FFmpeg: Seek input not output video FFmpeg: Improve performance by seeking input rather than output video Nov 20, 2023
@lastzero
Copy link
Member

@alexspurling I've tested it with a few large videos and it seems to be much faster now. Thank you very much! ❤️

@graciousgrey graciousgrey added tested Changes have been tested successfully and removed please-test Ready for acceptance test labels Nov 28, 2023
@srett
Copy link
Contributor

srett commented Nov 29, 2023

FWIW (but I don't think it really matters here), this makes the seek less accurate, as ffmpeg can only seek to the beginning of a GOP when not decoding, so you usually end up a few seconds before the desired timestamp... OTOH, this means we end up on an I-frame, which usually has slightly better quality.

@lastzero
Copy link
Member

FWIW (but I don't think it really matters here), this makes the seek less accurate, as ffmpeg can only seek to the beginning of a GOP when not decoding, so you usually end up a few seconds before the desired timestamp...

We value your feedback and insight! Happy to implement further improvements if needed.

OTOH, this means we end up on an I-frame, which usually has slightly better quality.

I agree, that's actually an advantage, as time accuracy is not important for this use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Refactoring, improvement or maintenance task merged Changes should be tested again after they have been integrated performance Performance Optimization tested Changes have been tested successfully video Video Formats, Transcoding, FFmpeg, Streaming & Co
Projects
Status: Release 🌈
Development

Successfully merging this pull request may close these issues.

None yet

5 participants