Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Announcements and Chat > General Discussion

Reply
 
Thread Tools Search this Thread Display Modes
Old 28th December 2018, 18:52   #1  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
FFmpeg CLI Help Guide

I've been learning and playing around with FFmpeg for the past month, specifically HDR to SDR tonemapping, asking questions over at this thread. There are still many questions I have, and from what I can tell there are not a whole lot of threads here for FFmpeg general discussion and use.

I figure this thread can be where for those of us that need advanced help with FFmpeg can ask the more experienced users questions. I'll throw a few links down below that have helped me get started as a beginner.

How to set up and use FFmpeg with Windows

Emulating MadVR's HDR to SDR tonemapping using FFMPEG

FFmpeg download link

FFmpeg wiki
EmKayVe is offline   Reply With Quote
Old 28th December 2018, 18:56   #2  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
Here is what I'm currently working on and need help with:

Attempting to convert an UHD Blu Ray rip from HDR to SDR:

ffmpeg.exe -i video.mkv -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -c:v libx265 -crf 18 -preset slower output.mkv

So far, the output is good. I would like to pass through the audio and adding "acodec copy" gives me the following "Too many packets buffered for output stream 0:1"

Any help with the CLI would be appreciated.
EmKayVe is offline   Reply With Quote
Old 29th December 2018, 04:54   #3  |  Link
Wakaku
MKV and tools user
 
Join Date: Apr 2014
Posts: 43
I might had also encountered such message myself, but I can't remember how I ended up with it.

What is the codec for the audio track of the source Video.mkv?

Perhaps a workaround is to disable the audio track during the encoding process instead of lossless copy, via the "-an" option. Then after you end up with the new MKV without the audio track, use MKVToolNix to mux that new video file plus the audio track from the source MKV file. I think MKVToolNix has more chance to remux such problematic muxes. Of course sometimes FFmpeg is the more tolerant.
Wakaku is offline   Reply With Quote
Old 29th December 2018, 05:01   #4  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
Quote:
Originally Posted by Wakaku View Post
I might had also encountered such message myself, but I can't remember how I ended up with it.

What is the codec for the audio track of the source Video.mkv?

Perhaps a workaround is to disable the audio track during the encoding process instead of lossless copy, via the "-an" option. Then after you end up with the new MKV without the audio track, use MKVToolNix to mux that new video file plus the audio track from the source MKV file. I think MKVToolNix has more chance to remux such problematic muxes. Of course sometimes FFmpeg is the more tolerant.
There are three audio codecs in the mkv file. I figured it would pick the first default audio track which is DTS-HD. Perhaps I should just encode instead since I was planning on taking the output and doing so in Handbrake.

How would I instead encode the following audio tracks into the final output using CLI in FFmpeg: AC3 5.1 at 384kbps & an AAC 2.0 at 128kbps.
EmKayVe is offline   Reply With Quote
Old 29th December 2018, 05:28   #5  |  Link
Wakaku
MKV and tools user
 
Join Date: Apr 2014
Posts: 43
Quote:
Originally Posted by EmKayVe View Post
There are three audio codecs in the mkv file. I figured it would pick the first default audio track which is DTS-HD. Perhaps I should just encode instead since I was planning on taking the output and doing so in Handbrake.
Indeed, I just tested now. I muxed together using MKVToolNix a video track plus 3 audio tracks in order: AAC, Vorbis, Opus. Then I remuxed that output file with FFmpeg (eg. ffmpeg -i Z.mkv -c copy Z2.mkv), and only the first audio track (which is the AAC) is retained.

Quote:
Originally Posted by EmKayVe View Post
How would I instead encode the following audio tracks into the final output using CLI in FFmpeg: AC3 5.1 at 384kbps & an AAC 2.0 at 128kbps.
Do you mean that you want to re-encode the original first audio track DTS-HD to both AC3 5.1 384kbps and AAC 2.0 128kbps?

Or the other two audio tracks from the source media file are those AC3 5.1 384kbps and AAC 2.0 128kbps, and you want to retain both of them?
Wakaku is offline   Reply With Quote
Old 29th December 2018, 06:05   #6  |  Link
Wakaku
MKV and tools user
 
Join Date: Apr 2014
Posts: 43
I just tested again, still using my previous Z.mkv (which contains the 3 audio tracks). To retain all 3 audio tracks with FFmpeg, I now used:

ffmpeg -i "Z.mkv" -c:v copy -map 0:v:0 -c:a copy -map 0:a:0 -map 0:a:1 -map 0:a:2 "Z3.mkv"

0:v:0 = 1st video track
0:a:0 = 1st audio track
0:a:1 = 2nd audio track
0:a:2 = 3rd audio track

I just found out too that if I used mappings for the audio tracks, then I must also use mapping for the video track, because if I do not the output would only be the 3 audio tracks inside the MKV.
Wakaku is offline   Reply With Quote
Old 29th December 2018, 08:28   #7  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
Quote:
Originally Posted by Wakaku View Post
Do you mean that you want to re-encode the original first audio track DTS-HD to both AC3 5.1 384kbps and AAC 2.0 128kbps?

Or the other two audio tracks from the source media file are those AC3 5.1 384kbps and AAC 2.0 128kbps, and you want to retain both of them?
Yes exactly (to the first part). I'd want to re-encode the original track to both AC3 & AAC. The other two tracks are a DTS track and AC3 track (but much higher bitrates than I would like).

Ideally, I want to be able to do this so that I don't have to turn to handbrake after an FFmpeg re-encode. I believe re-encoding an already re-encoded file is killing the quality more than if I just used FFmpeg to begin with.
EmKayVe is offline   Reply With Quote
Old 31st December 2018, 08:08   #8  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
If someone could help me with the CLI, as I have very little idea of what I'm doing . I'd like to change audio to include the following from the default track: AC3 5.1 384kbps and AAC 2.0 128kbps

Also, would like to encode to h264, 1080p, .mp4 - Thanks!

Just configure what I have already:

ffmpeg.exe -i video.mkv -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -c:v libx265 -crf 18 -preset slower output.mkv
EmKayVe is offline   Reply With Quote
Old 6th January 2019, 11:12   #9  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
You need to run 'ffmpeg -i video.mkv' to see the mapping ffmpeg assumes, based on that mapping adding (somewhere behind the loading of the inputs):
Code:
-c:a copy
would passthrough all audio streams from the source
Code:
-c:a copy -map 0:a:0
would passthrough only the first audio stream from your source
Code:
-c:a copy -map 0:a:1
would passthrough only the second audio stream from your source (run 'ffmpeg -i video.mkv' to see the mapping ffmpeg assumes)
Code:
-c:a copy -map 0:a:0 -map 0:a:1
would passthrough only the first and second audio stream from your source (run 'ffmpeg -i video.mkv' to see the mapping ffmpeg assumes)

Looking at:
Code:
-map A:a:B
A sets the index of the input to choose from
a sets that only audio streams should be looked at
B sets the index of the streams from the selection (here 'a' = audio)
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Old 6th January 2019, 12:42   #10  |  Link
sneaker_ger
Registered User
 
Join Date: Dec 2002
Posts: 5,565
Quote:
Originally Posted by Selur View Post
Code:
-c:a copy
would passthrough all audio streams from the source
If you provide no explicit -map then ffmpeg will only use the default mapping which means only one audio will be mapped.
"Passthrough all audio streams from first input file" is:
Code:
-map 0:a -c:a copy

Last edited by sneaker_ger; 6th January 2019 at 12:46.
sneaker_ger is offline   Reply With Quote
Old 6th January 2019, 18:00   #11  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
Awesome thank you both. To limit how many times I re-encode, could either of you provide the code for "AC3 5.1 384kbps and AAC 2.0 128kbps"? My target is taking the UHD/HDR down to FHD/SDR with those two aforementioned audio streams for remote Emby/Plex streams. This will limit the need for the server to transcode the file.

Also, does FFmpeg encode to .mp4? If so, how do I tell it to?
EmKayVe is offline   Reply With Quote
Old 6th January 2019, 18:05   #12  |  Link
sneaker_ger
Registered User
 
Join Date: Dec 2002
Posts: 5,565
-c:a ac3
(it will downmix to 5.1 if necessary since the ac3 encoder doesn't support more than 6 channels anyways)

-c:a aac -ac 2
For aac encoding with stereo downmix.

Note that both of these will re-encode even if source is already ac3/aac. So use -c:a copy if that is the case to preserve 100% quality.

mp4 output:
If the output file name ends with ".mp4" ffmpeg will automatically create an mp4 file.

Last edited by sneaker_ger; 6th January 2019 at 18:09.
sneaker_ger is offline   Reply With Quote
Old 6th January 2019, 18:55   #13  |  Link
qyot27
...?
 
qyot27's Avatar
 
Join Date: Nov 2005
Location: Florida
Posts: 1,419
I don't know if/how well FFmpeg can handle LFE mixing. Most of the time when I process my (4K or not) Blu-ray rips for watching on my desktop, I use eac3to to downmix and mix the LFE channel into the stereo signal (then encode the result to Opus and remux with the original video stream) so that when watching it back with mpv it doesn't cause nasty clipping when I try to use any volume boosting.

Of course, if you've got the right speaker setup, this is probably a completely moot point, but it is something to consider if not.
qyot27 is offline   Reply With Quote
Old 6th January 2019, 19:21   #14  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
Quote:
Originally Posted by sneaker_ger View Post
-c:a ac3
(it will downmix to 5.1 if necessary since the ac3 encoder doesn't support more than 6 channels anyways)

-c:a aac -ac 2
For aac encoding with stereo downmix.

Note that both of these will re-encode even if source is already ac3/aac. So use -c:a copy if that is the case to preserve 100% quality.

mp4 output:
If the output file name ends with ".mp4" ffmpeg will automatically create an mp4 file.
Quote:
Originally Posted by qyot27 View Post
I don't know if/how well FFmpeg can handle LFE mixing. Most of the time when I process my (4K or not) Blu-ray rips for watching on my desktop, I use eac3to to downmix and mix the LFE channel into the stereo signal (then encode the result to Opus and remux with the original video stream) so that when watching it back with mpv it doesn't cause nasty clipping when I try to use any volume boosting.

Of course, if you've got the right speaker setup, this is probably a completely moot point, but it is something to consider if not.
Ok got it thank you both for the help. The source is most likely TrueHD/Atmos (that is what I think all my UHDs are so far).

In regards to the LFE, I think the client would handle this if they have an AC3 capable player and if not the server will force the AAC instead.

So to recap my code will look like:

ffmpeg.exe -i video.mkv -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -c:a ac3 -c:a aac -ac 2 -c:v libx265 -crf 18 -preset slower output.mp4

ffmpeg.exe -i video.mkv -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -c:a ac3 -c:a aac -ac 2 -c:v libx265 -crf 18 -preset slower output.mp4

EDIT: It only encoded the red.

Last edited by EmKayVe; 6th January 2019 at 19:36.
EmKayVe is offline   Reply With Quote
Old 6th January 2019, 19:50   #15  |  Link
sneaker_ger
Registered User
 
Join Date: Dec 2002
Posts: 5,565
You want the same audio stream twice within the same output file? This wasn't clear to me from your other post. Then you have to map the same stream twice and use stream specifiers to tell ffmpeg which track the -c:a and -ac options apply to:
Code:
-map 0:v:0 -map 0:a:0 -map 0:a:0 -c:a:0 ac3 -c:a:1 aac -ac:a:1 2
Maps first video once, first audio twice.
sneaker_ger is offline   Reply With Quote
Old 6th January 2019, 19:56   #16  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
Quote:
Originally Posted by sneaker_ger View Post
You want the same audio stream twice within the same output file? This wasn't clear to me from your other post. Then you have to map the same stream twice and use stream specifiers to tell ffmpeg which track the -c:a and -ac options apply to:
Code:
-map 0:v:0 -map 0:a:0 -map 0:a:0 -c:a:0 ac3 -c:a:1 aac -ac:a:1 2
Maps first video once, first audio twice.
Yes sorry. I would like the output file to have two audio streams for the client to choose from. Both streams encoded from the 1st audio source.

So something like this should work right?

ffmpeg.exe -i D:\Videos\Media\Temp\video.mkv -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p,zscale=s=1920x1080 -map 0:v:0 -map 0:a:0 -map 0:a:0 -c:a:0 ac3 -c:a:1 aac -ac:a:1 2 -c:v libx265 -crf 20 -preset slower D:\Videos\Media\Temp\output.mp4
EmKayVe is offline   Reply With Quote
Old 6th January 2019, 20:00   #17  |  Link
microchip8
ffx264/ffhevc author
 
microchip8's Avatar
 
Join Date: May 2007
Location: /dev/video0
Posts: 1,843
it is not recommended to turn off desat (desat=0). This was derived from testing on the broken HDR sample from Sony (Camp fire). This sample is known to have screwed up HDR data, thus an incorrect conclusion was made that you should always disable desat
__________________
ffx264 || ffhevc || ffxvid || microenc
microchip8 is offline   Reply With Quote
Old 7th January 2019, 02:58   #18  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
Quote:
Originally Posted by froggy1 View Post
it is not recommended to turn off desat (desat=0). This was derived from testing on the broken HDR sample from Sony (Camp fire). This sample is known to have screwed up HDR data, thus an incorrect conclusion was made that you should always disable desat
Ok got it. Recommendations? (desat=?)
EmKayVe is offline   Reply With Quote
Old 7th January 2019, 05:09   #19  |  Link
EmKayVe
Registered User
 
EmKayVe's Avatar
 
Join Date: Dec 2018
Posts: 21
OK according to @Selur post here he is using desat=2. I have found his code to be significantly different than my HDR to SDR conversion code I've been using. I'm till learning gents... Can anyone decode the code for me? Would this be superior for an UHD/HDR conversion? Remember, I'm re-encoding "UHD - BD movies" to a target of 1080p/SDR/AC3 (.mp4).

Code:
ffmpeg -y -loglevel fatal -threads 8 -i "F:\TestClips&Co\files\HDR\4K sun HDR test.mp4" -map 0:0 -an -sn -vf zscale=tin=smpte2084:min=bt2020nc:pin=bt2020:rin=tv:t=smpte2084:m=bt2020nc:p=bt2020:r=tv,zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=2.000:peak=0.000,zscale=t=bt709:m=bt709:r=tv -pix_fmt yuv420p10le -strict -1 -vsync 0  -f yuv4mpegpipe - | x265 --input - --output-depth 10 --y4m --profile main10 --qg-size 8 --limit-modes --no-open-gop --opt-ref-list-length-pps --radl 2 --lookahead-slices 0 --crf 18.00 --opt-qp-pps --cbqpoffs -2 --crqpoffs -2 --limit-refs 0 --ssim-rd --psy-rd 2.50 --rdoq-level 2 --psy-rdoq 10.00 --aq-mode 0 --deblock=-1:-1 --limit-sao --range limited --colormatrix bt709 --output "E:\Temp\00_35_54_2910_01.265"
EmKayVe is offline   Reply With Quote
Old 7th January 2019, 19:06   #20  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
I used the default values there for desat&peak, see: https://www.ffmpeg.org/ffmpeg-all.html#tonemap-1
While at is has anyone tested tonemap_opencl ? (see: https://www.ffmpeg.org/ffmpeg-all.ht...map_005fopencl)
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 18:23.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.