Jump to content

Hardware encoded conversion - 4k content to 1080p on the Raspberry Pi


idiotinside

Recommended Posts

idiotinside

Hi, I'm currently evaluating on a monthly license whether Emby is usable as a media centre on the raspberry pi 4 8GB.

I am currently writing a script to convert my media library. The goal is to have no "on the fly" encoding by providing the original files to my devices who can play them and having a "universal" 1080p encode, which can be played on my older devices. I'm not using the builtin conversion feature for the following reasons:

Spoiler

- I can't "keep originals only if they are 4k, and always convert to 1080p"
- Converted files still can't play without conversion (5.1 audio gets converted on the fly to stereo when playing on my chromecast)
- It often fails or adds movies twice


- If the video file is in 4k, I copy it to my movie collection and suffix the filename with "- 4K". I want this file to be played for example on my LG smart TV.
- For my lower power devices (like chromecast 2, old FireTV stick) I want to create a 1080p version with stereo audio and pretty ok quality. (<-- This is where I'm currently having issues with.)

I chose what I believe to be a pretty universal format: h264 with AAC. I also want to do hardware encoding on the raspberry pi (-vcodec h264_v4l2m2m made it convert roughly at 0.25, which is usable for me)

This is what I came up with:

ffmpeg -i Terminator\ 2\ Judgement\ Day\ \(1991\)\ -\ 4K.mkv -vcodec h264_v4l2m2m -acodec aac -movflags +faststart -vf scale=1920:1080 Terminator\ 2\ Judgement\ Day\ \(1991\)\ -\ 1080p.mp4

However, when I try to play the resulting file in the browser, it is filled with green artifacts (see attachment). On the chromecast using the original emby app, it won't even start at all.

How can I make the 1080p encode play? Why is the file size of the encode exploded to 27 GB (as opposed to the 7GB 4k version)? This feels unintuitive to me 😕

 

Thanks in advance for your input :)

1080p encode in browser.png

mediainfo 4k original.PNG

mediainfo 1080p encode.PNG

Link to comment
Share on other sites

Hi there, when you open the video detail screen, which version is selected before you press play?

Link to comment
Share on other sites

idiotinside

Hey Luke, you mean over here?

image.thumb.png.7d266784af1f292c6d994a7cd2a02c85.png

I made sure to select the right version and tested again (in chrome), still the same effect:

  • 1080p -> Video with green artifacts (although you can somewhat see what's going on), audio is playing fine
  • 4k -> Emby converts the file on the fly (which works) but is ofc too slow for real time consumption and therefore stutters

When I click the download button to get the download link and paste the download link (https://********:1337/emby/Items/957/Download?api_key=********************************&mediaSourceId=bb854b315754317a3f2acc6b9aea1cfd) into vlc->open media->network, it only plays the audio.

(I couldn't download the entire file because my local hard disk is full)

Link to comment
Share on other sites

rbjtech

The 4K 'source' is a 7Mbit HEVC file - the resulting 'destination' is a 30Mbit AVC/H264 file.

You need to supply the h264 'profile' and type of encoding you want to use - crf,  fixed or 2 pass encoding etc.  There is an entire WORLD of options out there.

Have a look here for a quick insight -

https://trac.ffmpeg.org/wiki/Encode/H.264

To note - you also need to include tone mapping in the conversion - otherwise your resulting file will look very 'off' from a colour perspective.

Link to comment
Share on other sites

On 5/24/2022 at 3:33 AM, idiotinside said:

Hey Luke, you mean over here?

image.thumb.png.7d266784af1f292c6d994a7cd2a02c85.png

I made sure to select the right version and tested again (in chrome), still the same effect:

  • 1080p -> Video with green artifacts (although you can somewhat see what's going on), audio is playing fine
  • 4k -> Emby converts the file on the fly (which works) but is ofc too slow for real time consumption and therefore stutters

When I click the download button to get the download link and paste the download link (https://********:1337/emby/Items/957/Download?api_key=********************************&mediaSourceId=bb854b315754317a3f2acc6b9aea1cfd) into vlc->open media->network, it only plays the audio.

(I couldn't download the entire file because my local hard disk is full)

OK, let's look at an example of the green artifacts: 

Thanks !

 

Link to comment
Share on other sites

idiotinside

Hey Luke and rbjtech,

After looking through the ffmpeg documentation and some stackoverflow, I think I've found the reason for the green video output.

This guy is also getting green artifacts in his video files: https://www.willusher.io/general/2020/11/15/hw-accel-encoding-rpi4

He explains that it is due to the ffmpeg version being older than 4.3. My ffmpeg from the official repository seems to be even older than that!:

ffmpeg version 4.1.9-0+deb10u1+rpt1 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 8 (Raspbian 8.3.0-6+rpi1)

So the workaround for now for me was to just use the h264_OMX encoder. I know that it's deprecated but since I'm running on 32 bit raspbian it should not be a problem, right? I really would prefer to not compile ffmpeg by hand.

So this is working now. I have to however allow for every single user:
Allow audio playback that requires transcoding
- Allow video playback that requires conversion without re-encoding
for this to work because the audio has to be transcoded (to 2 channel, I presume), which the pi seems to be handling fine. See file attached.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Regarding the HDR tone mapping, I'm trying to encode with "-x264opts colorprim=bt2020:colormatrix=bt2020nc:transfer=smpte2084:chromaloc=2" now. Is this mapping universal tho? I really would like the command to be applicable to most of anything I throw at it, even things that don't have HDR.

This is the final command that I've come up with and which I'm going to run now (will take a few hours, I guess):

ffmpeg -i "Speed (1994) - 4k.mkv" -vcodec h264_omx -acodec aac -movflags +faststart -vf scale=1920:-2 -b:v 8000k -x264opts colorprim=bt2020:colormatrix=bt2020nc:transfer=smpte2084:chromaloc=2 "Speed (1994) - h264_HDRmapped.mp4"

I'm doing speed now, because I have deleted the terminator encode by accident, so I wouldn't be able to compare the colormapped version and the default one

embyplayback.PNG

Link to comment
Share on other sites

Quote

Is this mapping universal tho?

What do you mean by universal? If you're asking if you can use it with any video, I guess you'd just have to try it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...