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

libopus for channels >2 needs channel layout with -af #185

Closed
Tupsi opened this issue Jan 24, 2021 · 15 comments
Closed

libopus for channels >2 needs channel layout with -af #185

Tupsi opened this issue Jan 24, 2021 · 15 comments
Labels
bug Something isn't working

Comments

@Tupsi
Copy link

Tupsi commented Jan 24, 2021

You get an error in FastFlix if you try to encode 5.1/7.1 audio to opus.
If you try to encode audio with libopus for more then 2 channels, you have to add the parameter for channel layouts in ffmpeg like this:

for 5.1
-mapping_family 1 -af aformat=channel_layouts=5.1
for 7.1
-mapping_family 1 -af aformat=channel_layouts=7.1

so a complete 5.1 audio line would look like this:
-c:a libopus -b:a 256000 -mapping_family 1 -af aformat=channel_layouts=5.1

Currently Fastflix is not presenting this to ffmpeg, so an encode fails.

@Tupsi Tupsi changed the title libops for channels >2 needs mapping libops for channels >2 needs channel layout with -af Jan 24, 2021
@Tupsi Tupsi changed the title libops for channels >2 needs channel layout with -af libopus for channels >2 needs channel layout with -af Jan 24, 2021
@cdgriffith
Copy link
Owner

Thanks for the detailed info on the error and how to fix it!

It's probably a good idea long term to specify channel layout explicitly anyway, so that may be the true "fix".

I think mapping family can be ignored, as it should be auto-detected.

For time being, can add a custom audio filter for that track via the "custom ffmpeg options" field.

second_channel

i.e. if it is output track 2 (the one on the right side of the :)
-filter:2 aformat=channel_layouts=<layout>
layouts can be found https://ffmpeg.org/ffmpeg-utils.html#channel-layout-syntax

@cdgriffith cdgriffith added the bug Something isn't working label Jan 25, 2021
@Tupsi
Copy link
Author

Tupsi commented Jan 25, 2021

I just tested it on the command line and yes, the mapping param can be ignored as it seems to autodetect it just fine (0 = stereo/mono, 1 = channel >2).
And fixed my initial entry, of course you have to put 7.1 in for 7.1 channels, silly copy&paste error.

@Tupsi
Copy link
Author

Tupsi commented Jan 25, 2021

and thanks for the work around! Was not aware that you can put the format in there! Is there also a way to adjust the bitrate of audio somewhere besides your fixed values? the opus team itself recommands only 450k bitrate for 7.1, so the current options are either not enough or to much. :-) I know, luxury problems. :-)

@cdgriffith
Copy link
Owner

Sorry I missed the follow up comment! Currently it's not modifiable, however I will switch from using a 32x multiplier to a 16x one for bitrate selection ranges, as I have had multiple people request more options for that. Long term I want to make it customizable like CRF / Bitrate for video.

cdgriffith added a commit that referenced this issue Feb 14, 2021
* Adding #109 NVENC HEVC support based on FFmpeg
* Adding NVEenC encoder for HEVC and AVC
* Adding #166 More robust queue that is recoverable
* Adding ability to extract HDR10+ metadata if hdr10plus_parser is detected on path
* Adding #178 selector for number of autocrop positions throughout video (thanks to bmcassagne)
* Adding Windows 10 notification for queue complete success
* Adding #194 fast two pass encoding (thanks to Ugurtan)
* Fixing German translations (thanks to SMESH)
* Fixing #171 Be able to select encoder before selecting video
* Fixing #176 Unable to change queue order or delete task from queue since 4.1.0 (thanks to Etz)
* Fixing #185 need to specify channel layout when downmixing (thanks to Ugurtan)
* Fixing #187 cleaning up partial download of FFmpeg (thanks to Todd Wilkinson)
* Fixing #190 add missing chromaloc parameter for x265 (thanks to Etz)
* Fixing that returning item back from queue of a different encoder type would crash Fastflix
* Fixing HDR10 details to be track specific (thanks to Harybo)
@Alkl58
Copy link

Alkl58 commented Mar 3, 2021

Also btw, which is a easier solution:
If audio gets encoded, all you need to specify is:

-af aformat=channel_layouts="7.1|5.1|stereo|mono"

It will correctly choose the channel layout combined with -ac, as the | acts as "or" statement.

It simplifies the code, idk. if you want to change the current implementation.

@cdgriffith
Copy link
Owner

@Alkl58 that is a cool tidbit, I did not know that!

Actually in the latest beta I just have you select the actual channel layout itself, as that seemed easiest lol (there are a few odd ones missing, as it's the union between what's supported with ffmpeg and the other NVEncC encoder)

cdgriffith added a commit that referenced this issue Apr 13, 2021
* Adding #109 NVENC HEVC support based on FFmpeg (thanks to Zeid164)
* Adding NVEenC encoder for HEVC and AVC
* Adding #166 More robust queue that is recoverable
* Adding ability to extract HDR10+ metadata if hdr10plus_parser is detected on path
* Adding #178 selector for number of autocrop positions throughout video (thanks to bmcassagne)
* Adding Windows 10 notification for queue complete success
* Adding #194 fast two pass encoding (thanks to Ugurtan)
* Adding Confirm dialogue for cancel encode and replace currently working on video
* Changing AVC defaults and recommendations for CRF to higher values
* Changing VP9 to default to mkv instead of webm format to support more audio codecs
* Fixing German translations (thanks to SMESH)
* Fixing #171 Be able to select encoder before selecting video
* Fixing #176 Unable to change queue order or delete task from queue since 4.1.0 (thanks to Etz)
* Fixing #185 need to specify channel layout when downmixing (thanks to Ugurtan)
* Fixing #187 cleaning up partial download of FFmpeg (thanks to Todd Wilkinson)
* Fixing #190 add missing chromaloc parameter for x265 (thanks to Etz)
* Fixing #209 Double spaces were removed in incoming filenames, causing no file found (thanks to stilicrafter)
* Fixing that deinterlace detection could crash program due to CPython bug issue #43423 (thanks to macx)
* Fixing that returning item back from queue of a different encoder type would crash Fastflix
* Fixing HDR10 details to be track specific (thanks to Harybo)
* Fixing returning from queue works with duplicated audio tracks

Co-authored-by: SMESH <4199206+smesh109@users.noreply.github.com>
Co-authored-by: TGMais <2453844+tgmais@users.noreply.github.com>
@cdgriffith
Copy link
Owner

@djsat2
Copy link

djsat2 commented Jun 6, 2021

Hello, sorry if this is the wrong place to post this but despite having the newest version of FastFlix i still can't transcode some 5.1 streams into Opus as i get the above mentioned error. If there is only one 5.1 sound track and im transcoding to Opus 5.1 I can use the above mentioned command line options and the audio will be converted but leaving that out or more than one audio track and i still get the error ... extra annoying as it happens after all the 8+ hours of video compression as been done. Am i doing something wrong? or have I missed something? Thanks.

@cdgriffith
Copy link
Owner

Hi @djsat2 can you share what the command is that you are trying to run and the error it is giving? (Please remove any sensitive content / file names)

Also remember you can always encode the audio separately than the video and remux them together later, so you don't have to just throw the encoded video away! (Requires re-encoding just audio with FFmpeg then using a muxer, either FFmpeg on the command line or others)

@djsat2
Copy link

djsat2 commented Jun 9, 2021

Hello, i replicated the error using a sample freebie video with 5.1 channel sound and used NVENC as the codec for speed (normally use x265), managed to replicate the same error by injecting "-mapping_family 1 -af aformat=channel_layouts=5.1" using the 'Custom FFMPEG options" field.

The commands are (2-pass):

"C:\Users\XXXXXX\AppData\Roaming\FFmpeg\bin\ffmpeg.exe" -y -i "C:\Video\Tests\dts_orchestra_long_core_1080p-thedigitaltheater.mkv" -max_muxing_queue_size 1024 -filter_complex "[0:0]crop=1920:1072:0:4[v]" -map "[v]" -c:v hevc_nvenc -pix_fmt p010le -tune:v hq -spatial_aq:v 0 -tier:v main -rc-lookahead:v 0 -gpu -1 -b_ref_mode disabled -profile:v main -pass 1 -passlogfile "C:\Users\XXXXXX\AppData\Roaming\FastFlix\temp_1e3d6b4bfaa07b007def4750\pass_log_file_88d1f86ea6a2df725002" -b:v 6000k -preset:v slow -2pass 1 -an -sn -dn -f mp4 NUL

"C:\Users\XXXXXX\AppData\Roaming\FFmpeg\bin\ffmpeg.exe" -y -i "C:\Video\Tests\dts_orchestra_long_core_1080p-thedigitaltheater.mkv" -max_muxing_queue_size 1024 -filter_complex "[0:0]crop=1920:1072:0:4[v]" -map "[v]" -c:v hevc_nvenc -pix_fmt p010le -tune:v hq -spatial_aq:v 0 -tier:v main -rc-lookahead:v 0 -gpu -1 -b_ref_mode disabled -profile:v main -pass 2 -passlogfile "C:\Users\XXXXX\AppData\Roaming\FastFlix\temp_1e3d6b4bfaa07b007def4750\pass_log_file_88d1f86ea6a2df725002" -2pass 1 -b:v 6000k -preset:v slow -mapping_family 1 -af aformat=channel_layouts=5.1 -map_metadata -1 -map_chapters 0 -map 0:1 -metadata:s:1 title="" -metadata:s:1 handler="" -c:1 libopus -b:1 384k -default_mode infer_no_subs "C:\Video\Tests\dts_orchestra_long_core_1080p-thedigitaltheater-fastflix-b619.mkv"

From looking at the log file, the errors are:

2021-06-09 14:15:44,201 - [libopus @ 00000196d82668c0] Invalid channel layout 5.1(side) for specified mapping family -1.
2021-06-09 14:15:44,203 - Error initializing output stream 0:1 -- Error while opening encoder for output stream #0:1 - maybe incorrect parameters such as bit_rate, rate, width or height
2021-06-09 14:15:44,207 - Conversion failed!

Let me know if you need anything more from me.

@cdgriffith
Copy link
Owner

Ok so it looks like the issue here is that when not downmixing to less tracks, aka keeping it at 5.1, FastFlix does not inject a channel layout and FFmpeg for some reason doesn't provide that information to the libopus library.

You were on the right track to try to put that in yourself, however the FFmpeg arguments are location dependent, and where FastFlix injects the additional ones for the video will not affect the audio track (notice the af aformat=channel_layouts=5.1 comes before -map 0:1)

For example can replicate with that source by trying:

ffmpeg -i .\dts_orchestra_long_core_1080p-thedigitaltheater.mkv -map 0:1 -c:a libopus -b:a 384k -af aformat=channel_layouts=5.1 out.mkv

Whereas without the channel layout it will fail:

ffmpeg -i .\dts_orchestra_long_core_1080p-thedigitaltheater.mkv -map 0:1 -c:a libopus -b:a 384k out.mkv
... 
[libopus @ 00000269d9efc580] Invalid channel layout 5.1(side) for specified mapping family -1.
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!

Reopening this issue, will have to provide channel information to at least libopus even when not downmixing , which FFmpeg should REALLY handle by itself :(

@cdgriffith cdgriffith reopened this Jun 9, 2021
@cacafuti8888
Copy link

Hello, i also notice that issue. And changing the audio channels also reset to default the bitrate which is very annoying, so if you change first the bitrate to anything that's not the default (i think it's 384K) and then you change the channels it will switch back to the default bitrate. Very annoying when you check the output file and realize that audio is converted with an undesired bitrate

@Arc-2023
Copy link

Arc-2023 commented Jul 25, 2023

well i met that too, wondering why doesn't it get fixed after such a long duration since issue created

cdgriffith added a commit that referenced this issue Aug 6, 2024
* Adding #536 Improve Profiles - save advanced options (thanks to CelticTaonga and DCNerds)
* Fixing #185 audio channels not being set properly and resetting on encoder change (thanks to Tupsi)
* Fixing #522 add file fails - fixed as of 5.7.0 (thanks to pcl5x2008)
* Fixing #531 list limitation in readme that FFmpeg must support the software encoders listed (thanks to brunoais)
@cdgriffith
Copy link
Owner

Making sure to add manual channel layout for everything in 5.8.0. Beta to test https://github.com/cdgriffith/FastFlix/releases/tag/5.8.0b0

cdgriffith added a commit that referenced this issue Aug 9, 2024
* Adding #283 support for experimental DTS (dca) audio by adding -strict -2 (thanks to Sub7)
* Adding #354 M1 support (thanks to Nhunz and Anton)
* Adding #536 Improve Profiles - save advanced options (thanks to CelticTaonga and DCNerds)
* Adding #568 center app on startup (thanks to Viet-Duc Le)
* Adding #589 support for pydantic 2.x (thanks to dmo marillat)
* Adding #592 Add alpha channel for VP9 (thjanks to subof)
* Fixing #185 audio channels not being set properly and resetting on encoder change (thanks to Tupsi)
* Fixing #522 add file fails - fixed as of 5.7.0 (thanks to pcl5x2008)
* Fixing #531 list limitation in readme that FFmpeg must support the software encoders listed (thanks to brunoais)
* Fixing #567 Profiles for WebP did not work (nor GIF dither) (thanks to jpert)
* Fixing #582 BT.2020-10 Color transfer not maintained (thanks to Ryushin)
* Fixing #585 error when trying to return a video from queue that has the video track after audio or subtitiles (thanks to Hankuu)
* Fixing #586 audio channels being set incorrectly (thanks to Hankuu)
* Fixing #588 audio and subtitle dispositions were not set from source (thanks to GeZorTenPlotZ)
@cdgriffith
Copy link
Owner

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

No branches or pull requests

6 participants