discord / lilliput

Resize images and animated GIFs in Go
https://discord.com/blog/how-discord-resizes-150-million-images-every-day-with-go-and-c
Other
1.94k stars 124 forks source link

Attempt to retrieve video metadata without a frame decode. #145

Closed skidder closed 4 months ago

skidder commented 4 months ago

Speed up initialization of the avcodec decoder by calling avformat_find_stream_info only when required metadata is not present in the media file header (rare, i.e. mpeg-ts).

The avformat_find_stream_info function can perform a video frame decode, which offers increased accuracy at the expense of additional compute & latency. We will still call avformat_find_stream_info when metadata is not known, but it's not the common case.

Testing with a 14MB mp4 file that was previously causing performance issues showed a 675x speed-up (0.882 ms vs 592 ms):

Before

goos: linux
goarch: amd64
pkg: github.com/discord/lilliput
cpu: Intel(R) Xeon(R) CPU @ 3.10GHz
BenchmarkNewDecoder-16             2     596281993 ns/op         288 B/op         11 allocs/op
PASS
coverage: 7.1% of statements
ok      github.com/discord/lilliput 1.816s

After

goos: linux
goarch: amd64
pkg: github.com/discord/lilliput
cpu: Intel(R) Xeon(R) CPU @ 3.10GHz
BenchmarkNewDecoder-16          1448        882478 ns/op         286 B/op         10 allocs/op
PASS
coverage: 7.1% of statements
ok      github.com/discord/lilliput 1.446s