PeerTube icon indicating copy to clipboard operation
PeerTube copied to clipboard

Low CPU usage when transcoding lower resolutions

Open vid-bin opened this issue 3 years ago • 12 comments

Peertube uses 100% of the CPU when transcoding the original resolution but only 30%-50% of the CPU when it starts to transcode lower resolutions.

This is not version specific like I originally thought.

Additional information

  • PeerTube instance:
    • URL: vidbin.org
    • Version: 4.3.0-rc.1
    • NodeJS version: 18
    • Ffmpeg version: master

vid-bin avatar Sep 12 '22 07:09 vid-bin

I think what's going on here is the original video is only used for the first transcode. Then it uses the peertube produced HLS file for lower resolution transcodes. And for some reason it's just slower with the fragmented/HLS video.

vid-bin avatar Sep 12 '22 08:09 vid-bin

I observed similar behavior in my instance of PeerTube. Anything but highest resolution only maxes out at 60% CPU utilization, possibly leading to slower transcode times.

emansom avatar Sep 12 '22 14:09 emansom

https://www.youtube.com/watch?v=bR8De0woh0s

https://vidbin.org/w/4MsoK5aQdTxVkdqNFJDgfo

Using the below as the test:

https://vidbin.org/download/streaming-playlists/hls/videos/1ea4267d-37e3-4d40-bb92-124dd8faf1a6-1440-fragmented.mp4

Command:

ffmpeg -i /root/1ea4267d-37e3-4d40-bb92-124dd8faf1a6-1440-fragmented.mp4 -y -acodec aac -vcodec libsvtav1 -threads 24 -f mp4 -movflags faststart -max_muxing_queue_size 1024 -map_metadata -1 -pix_fmt yuv420p -b:a 192k -ac 2 -vf scale=w=-2:h=1080 -crf 42 -preset 7 -g 999 -hls_time 4 -hls_list_size 0 -hls_playlist_type vod -hls_segment_filename /root/1080-fragmented.mp4 -hls_segment_type fmp4 -f hls -hls_flags single_file /root/1080.m3u8

The resulting CPU is only 700% max.

vid-bin avatar Sep 12 '22 19:09 vid-bin

Using 308+251 (1440p+opus) and converting to 1080p.

ffmpeg -i /root/1440p.mkv -y -acodec aac -vcodec libsvtav1 -threads 24 -f mp4 -movflags faststart -max_muxing_queue_size 1024 -map_metadata -1 -pix_fmt yuv420p -b:a 192k -ac 2 -vf scale=w=-2:h=1080 -crf 42 -preset 7 -g 999 /root/output-1080p-fragmented.mp4

CPU usage is consistent at 2000%

vid-bin avatar Sep 12 '22 19:09 vid-bin

Recently discovered ffmpeg also supports transcoding to multiple resolutions at once, this would possibly yield the lowest transcoding time and most effective CPU utilization possible.

emansom avatar Sep 12 '22 20:09 emansom

ffmpeg's "trasncode to multiple resolutions at once" is actually just multiple outputs (i've encountered it before). however, it is not guaranteed to reduce transcoding time and could be potentially worse with it (see "Parallel Encoding" in the linked page).

JacksonChen666 avatar Sep 12 '22 20:09 JacksonChen666

Hello,

Is it a peertube bug? I think we always specify the appropriate thread option. Can you check the ffmpeg command ran by peertube?

Chocobozzz avatar Sep 13 '22 07:09 Chocobozzz

Hello,

Is it a peertube bug? I think we always specify the appropriate thread option. Can you check the ffmpeg command ran by peertube?

These are the commands run by peertube. I just don't understand why the first transcode doesn't have the hls options and subsequent transcodes with the lower resolutions do.

vid-bin avatar Sep 13 '22 10:09 vid-bin

After further testing the CPU usage is only limited to 700% when using the peertube-created 1440-fragmented.mp4 to encode lower resolutions.

Purposed solution: Use the original video for all resolutions. Currently peertube does the following:

original video (1440p) -> encoded/hls video (1440p) -> lower resolution/video/hls

Change to:

original video (1440p) -> encoded video/hls (1440p) && original video (1440p) -> lower resolution video/hls (1080p, 720p, etc)

It's not limited to 1440p. It does the same thing if 1080p is the max resolution. It always encodes the highest first, then uses the encoded video for lower resolutions.

vid-bin avatar Sep 13 '22 20:09 vid-bin

Isn't related to your encoder? Why ffmpeg consumes less CPU depending on the input file?

Chocobozzz avatar Sep 14 '22 07:09 Chocobozzz

Isn't related to your encoder? Why ffmpeg consumes less CPU depending on the input file?

@emansom Are you seeing the same thing on your end? You don't use svtav1 right?

vid-bin avatar Sep 14 '22 14:09 vid-bin

@emansom Are you seeing the same thing on your end? You don't use svtav1 right?

Yes, during channel import of 200+ videos it was 100% for the initial video and then 60-70% cycles of CPU usage after. Default settings. x264.

Schermafdruk van 2022-09-14 18-00-04

emansom avatar Sep 14 '22 16:09 emansom

@vid-bin Why don't you consider that transcoding from fragmented mp4 file with a scale filter resulting in low CPU usage is not a bug? https://trac.ffmpeg.org/ticket/9923

Chocobozzz avatar Sep 20 '22 07:09 Chocobozzz

Closing as it seems to be a ffmpeg bug rather than a peertube one

Chocobozzz avatar Oct 10 '22 13:10 Chocobozzz