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. |
|
28th May 2017, 21:13 | #1 | Link | ||
Registered User
Join Date: May 2004
Posts: 47
|
Optimal encoding with FFmpeg for DVD
I want to convert mp4 videos (typically around 135 mins) to mpeg2 with best quality possible while still fitting on single layer DVDs.
I found the parameter below in another thread. Quote:
I experimented, comparing it to this WinFF DVD preset: Quote:
But the first one produces a framerate of 25fps, and the 2nd one produces 29.97fps. Which is best for DVD? (my source files are 30fps). Also, I've read that 2-pass encodes can be higher quality and will be sized fit the DVD more precisely. How do I specify in the parameter that it should be 2-pass? I already know how to calculate bitrates and author DVDs, so ultimately I'm just looking for the optimum 2-pass VBR parameter for what I'd like to do. |
||
28th May 2017, 22:32 | #2 | Link |
Registered User
Join Date: Sep 2003
Location: Berlin, Germany
Posts: 3,105
|
Hi pojke,
I made ffmpeg_VBR plugins for AVStoDVD and for DVDStyler a while ago, both have the option to use an "Extremely High Quality" 2-pass VBR mode for FFmpeg. For DVDStyler my plugin is transparently integrated into the software, so this might be the easiest way to use this mode. The FFmpeg parameters for this mode were basically developed by forum member Fishman0919, and they were optimized further with the help of some DVDStyler forum members. If you prefer to use FFmpeg from the command line, here is the basic script: Code:
ffmpeg.exe -i %in% %aspect% %threads% %filter% %size% %dc% -g 12 -bf 2 -maxrate 8000k -q:v 2 %first_hq% %CQM% -pass 1 -passlogfile "%out_path%ffmpeg" -an -c:v mpeg2video %il% "%out_path%pass1.m2v" ffmpeg.exe -i %in% %aspect% %threads% %filter% %size% %dc% -g 12 -bf 2 -lmin 0.75 -mblmin 50 -qmin 1 -qmax 31 -maxrate 8000k -b:v %bitrate%k %second_hq% %CQM% -pass 2 -passlogfile "%out_path%ffmpeg" -an -c:v mpeg2video %il% %out% Code:
:2pass IF %bitrate% GTR 7990 SET bitrate=7990 IF EXIST "%out_path%ffmpeg*.log" DEL "%out_path%ffmpeg*.log" IF EXIST "%out_path%pass1.m2v" DEL "%out_path%pass1.m2v" IF [%interlaced%==[1 SET il=-flags +ilme+ildct -alternate_scan 1 -top 1 IF [%bff%==[1 SET il=-flags +ilme+ildct -alternate_scan 1 -top 0 IF NOT [%Force 2-pass VBR%==[1 IF NOT [%Force 2-pass VBR%==[2 IF EXIST "Fox New.txt" SET /P CQM=<"Fox New.txt" IF NOT [%Force 2-pass VBR%==[1 SET first_hq=-b_strategy 2 -brd_scale 2 -profile:v 4 SET second_hq=-pre_dia_size 5 -dia_size 5 -qcomp 0.7 -qblur 0 -preme 2 -me_method dia -sc_threshold 0 -sc_factor 4 -bidir_refine 4 -profile:v 4 -mbd rd -mbcmp satd -precmp satd -cmp satd -subcmp satd -skipcmp satd SET user_hq=%user_hq:-bf 2 =% IF [%Force 2-pass VBR%==[2 SET second_hq=%user_hq% IF [%Force 2-pass VBR%==[1 SET second_hq= Cheers manolito |
29th May 2017, 01:26 | #3 | Link |
Registered User
Join Date: May 2004
Posts: 47
|
Thanks, but I'm kind of lost with some of that because I'm not familiar with AVSTODVD or Styler. I like authoring DVDs with DVDLab Pro, so I'm not really looking for a complete solution. For this I just want to encode with command line FFmpeg.
I see now that it's your parameter that I quoted above. Is that particular one not best for what I'm doing? If not, could you put a modified one in the form that is accepted by the command line? I don't know what's meant by "%threads% %filter%" etc. |
29th May 2017, 01:48 | #4 | Link |
Registered User
Join Date: Sep 2003
Location: Berlin, Germany
Posts: 3,105
|
OK, you need to post your source properties (the MediaInfo information), plus the target DVD format (PAL or NTSC, progressive or interlaced, and the average video bitrate). I can then post an FFmpeg command line which will be adapted to your needs.
Cheers manolito |
29th May 2017, 03:20 | #5 | Link | |
Registered User
Join Date: May 2004
Posts: 47
|
It's an mp4 with a codec ID "isom". Audio is 112K AAC. I want to encode to NTSC (720x480) and I assume progressive (isn't that normal?). For the 1st video, the time is 2:13:05, so I calculated a bitrate of 4250 so that it would fit with 192K ac3 audio.
I didn't know the source mattered. I just plugged in the bitrate for video and audio, resolution and file name when trying the parameter I mentioned. Without the matrix stuff, it looked like this: Quote:
|
|
29th May 2017, 21:03 | #6 | Link |
Registered User
Join Date: Sep 2003
Location: Berlin, Germany
Posts: 3,105
|
Alright, here are the FFmpeg calls for a 2-pass VBR encode using parameters for "Extremely High Quality".
Convert.bat Code:
ffmpeg.exe -i "I:\test.mp4" -f mpeg2video -c:v:0 mpeg2video -b_strategy 2 -brd_scale 2 -profile:v 4 -intra_matrix "8,8,9,9,10,10,11,11,8,9,9,10,10,11,11,12,9,9,10,10,11,11,12,12,9,10,10,11,11,12,13,13,10,10,11,11,12,13,13,14,10,11,11,12,13,13,14,15,11,11,12,13,13,14,15,15,11,12,12,13,14,15,15,16" -inter_matrix "8,8,9,9,10,10,11,11,8,9,9,10,10,11,11,12,9,9,10,10,11,11,12,12,9,10,10,11,11,12,13,13,10,10,11,11,12,13,13,14,10,11,11,12,13,13,14,15,11,11,12,13,13,14,15,15,11,12,12,13,14,15,15,16" -aspect 16:9 -s 720x480 -r 24000/1001 -g 12 -pix_fmt yuv420p -b:v:0 7990000 -maxrate:v:0 8000000 -dc 10 -bf 2 -q:v 2 -bufsize:v:0 1835008 -packetsize 2048 -muxrate 10080000 -vf scale=718:480,pad=720:480:1:0 -map 0:v:0 -an -passlogfile "I:\ffmpeg" -pass 1 -y "NUL.avi" ffmpeg.exe -i "I:\test.mp4" -f mpeg2video -c:v:0 mpeg2video -pre_dia_size 5 -dia_size 5 -qcomp 0.7 -qblur 0 -preme 2 -me_method dia -sc_threshold 0 -bidir_refine 4 -profile:v 4 -mbd rd -mbcmp satd -precmp satd -cmp satd -subcmp satd -skipcmp satd -intra_matrix "8,8,9,9,10,10,11,11,8,9,9,10,10,11,11,12,9,9,10,10,11,11,12,12,9,10,10,11,11,12,13,13,10,10,11,11,12,13,13,14,10,11,11,12,13,13,14,15,11,11,12,13,13,14,15,15,11,12,12,13,14,15,15,16" -inter_matrix "8,8,9,9,10,10,11,11,8,9,9,10,10,11,11,12,9,9,10,10,11,11,12,12,9,10,10,11,11,12,13,13,10,10,11,11,12,13,13,14,10,11,11,12,13,13,14,15,11,11,12,13,13,14,15,15,11,12,12,13,14,15,15,16" -aspect 16:9 -s 720x480 -r 24000/1001 -g 12 -pix_fmt yuv420p -b:v:0 7990000 -maxrate:v:0 8000000 -dc 10 -bf 2 -lmin 0.75 -mblmin 50 -qmin 1 -bufsize:v:0 1835008 -packetsize 2048 -muxrate 10080000 -vf scale=718:480,pad=720:480:1:0 -map 0:v:0 -passlogfile "I:\ffmpeg" -pass 2 "I:\test.m2v" -f ac3 -b:a 192000 -ar 48000 -c:a:0 ac3 -map 0:a:0 "I:\test.ac3" IF EXIST "I:\ffmpeg*.log" DEL "I:\ffmpeg*.log" All the bold parts need to be modified for different sources and targets. This one is only for progressive sources. For target NTSC it encodes to 23.976 fps, pulldown must be added by your authoring application. If your source file is 30fps progressive then you might get smoother results by replacing "24000/1001" with "30000/1001". This will avoid the fps conversion from 30fps to 23.976fps which is performed by FFmpeg by just dropping whole frames. My test source had a frame size of 426x240, to keep the correct aspect ratio some padding was added using the "-vf scale" and "pad" parameters. You need to calculate these values manually for each different source. Depending on your source frame size you might be able to remove the whole "-vf" parameter. You just need to save this code as a batch file and put this batch file into a folder where you have a current version of FFmpeg. Good luck manolito Last edited by manolito; 11th September 2019 at 15:12. |
30th May 2017, 17:24 | #7 | Link |
Registered User
Join Date: May 2004
Posts: 47
|
Thank you! That seems to work well. The .bat file explanation was important. I had been using ffmpeg by pasting the parameters into the command line and couldn't figure out how to do that with 2 passes.
Some remaining questions: The output bitrate was 4195 for my test clip, which is below the 4250 that I specified. Is such a difference normal? Less is OK, but I wouldn't want it to go over the DVD size. Does the location of the passlog file matter? Can it be in a separate location from the source/output/ffmpeg ? Is it possible to use the other method (pasting the parameter into the command line) with 2-pass encoding? |
30th May 2017, 22:46 | #8 | Link |
Registered User
Join Date: Sep 2003
Location: Berlin, Germany
Posts: 3,105
|
Good to hear that it works for you...
IMO the requested output bitrate is generally met quite well in FFmpeg 2-pass VBR mode. It did happen a couple of times for me that the bitrate was a little lower, and the reason was that the source movie had end credits which did not require many bits. FFmpeg did use a quantizer of 0 for these end credits, but it was not enough to bring the overall bitrate up to the requested value. The passlog file can be anywhere on your HDD. Just don't delete it before finishing the second pass. Pasting the FFmpeg params into the command line is possible, but not for both passes at once. You need to paste the params for the first pass first, then run the first pass. After the first pass has finished you can paste the params for the second pass and execute it. I think using a batch file which runs both passes with one command is easier... To make it even easier I modified the batch file for much better usability: Convert.bat Code:
@ECHO off ==================================================== SET aspect=16:9 SET size=720x480 REM SET vfilter=-vf scale=718:480,pad=720:480:1:0 SET framerate=29.97 SET VBitrate=8000k SET logfile=%temp%\ffmpeg SET ABitrate=192k SET AChannels=2 ==================================================== SET FF_DRIVE=%~d0 SET FF_PATH=%~p0 %FF_DRIVE% CD "%FF_PATH%" SET in="%~1" SET in_path=%~dp1 SET in_name=%~n1 SET Vout="%in_path%%in_name%.m2v" SET Aout="%in_path%%in_name%.ac3" ffmpeg.exe -i %in% -f mpeg2video -c:v:0 mpeg2video -b_strategy 2 -brd_scale 2 -profile:v 4 -intra_matrix "8,8,9,9,10,10,11,11,8,9,9,10,10,11,11,12,9,9,10,10,11,11,12,12,9,10,10,11,11,12,13,13,10,10,11,11,12,13,13,14,10,11,11,12,13,13,14,15,11,11,12,13,13,14,15,15,11,12,12,13,14,15,15,16" -inter_matrix "8,8,9,9,10,10,11,11,8,9,9,10,10,11,11,12,9,9,10,10,11,11,12,12,9,10,10,11,11,12,13,13,10,10,11,11,12,13,13,14,10,11,11,12,13,13,14,15,11,11,12,13,13,14,15,15,11,12,12,13,14,15,15,16" -aspect %aspect% -s %size% -r %framerate% -g 12 -pix_fmt yuv420p -b:v:0 %VBitrate% -maxrate:v:0 8500000 -dc 10 -bf 2 -q:v 2 -bufsize:v:0 1835008 -packetsize 2048 -muxrate 10080000 %vfilter% -map 0:v:0 -an -passlogfile "%logfile%" -pass 1 -y "NUL.avi" ffmpeg.exe -i %in% -f mpeg2video -c:v:0 mpeg2video -pre_dia_size 5 -dia_size 5 -qcomp 0.7 -qblur 0 -preme 2 -me_method dia -sc_threshold 0 -sc_factor 4 -bidir_refine 4 -profile:v 4 -mbd rd -mbcmp satd -precmp satd -cmp satd -subcmp satd -skipcmp satd -intra_matrix "8,8,9,9,10,10,11,11,8,9,9,10,10,11,11,12,9,9,10,10,11,11,12,12,9,10,10,11,11,12,13,13,10,10,11,11,12,13,13,14,10,11,11,12,13,13,14,15,11,11,12,13,13,14,15,15,11,12,12,13,14,15,15,16" -inter_matrix "8,8,9,9,10,10,11,11,8,9,9,10,10,11,11,12,9,9,10,10,11,11,12,12,9,10,10,11,11,12,13,13,10,10,11,11,12,13,13,14,10,11,11,12,13,13,14,15,11,11,12,13,13,14,15,15,11,12,12,13,14,15,15,16" -aspect %aspect% -s %size% -r %framerate% -g 12 -pix_fmt yuv420p -b:v:0 %VBitrate% -maxrate:v:0 8500000 -dc 10 -bf 2 -lmin 0.75 -mblmin 50 -qmin 1 -bufsize:v:0 1835008 -packetsize 2048 -muxrate 10080000 %vfilter% -map 0:v:0 -passlogfile "%logfile%" -pass 2 %Vout% -f ac3 -b:a %Abitrate% -ar 48000 -ac %AChannels% -c:a:0 ac3 -map 0:a:0 %Aout% IF EXIST "%logfile%*.log" DEL "%logfile%*.log" Now you can set all your parameters right at the top of the file. The input clip is the first (and only) parameter for the batch file. This makes it possible to Copy&Paste or Drag&Drop the input clip on the batch file (or on a link to the batch file on your desktop). Enjoy manolito |
31st May 2017, 06:37 | #9 | Link |
Registered User
Join Date: May 2004
Posts: 47
|
It's good to know I don't have to worry about the warnings. They have also shown up in the past during mp4 encodings.
You are correct, this is my first experience with batch files (other than clicking the ff-prompt.bat file). I'll try to learn more about the syntax and rules as it might be more convenient than what I've been doing. Thanks for all the examples and explanations. Much appreciated. |
4th June 2017, 00:14 | #10 | Link |
Registered User
Join Date: Apr 2010
Location: I have a statue in Hakodate, Japan
Posts: 754
|
Hello manolito:
I was watching your configuration and I wonder if this way FFMPEG handles well the scenes with high movement. That's why I used the option -sc_threshold that gave me good results, like this, for example in a PC with 2 cores: Code:
ffmpeg -i "C:\input.avs" -threads 2 -pass 1 -passlogfile Paso1.log -vcodec mpeg2video -b:v 2700000 -maxrate 9800000 -bufsize 1835000 -g 12 -bf 2 -sc_threshold -25000 -b_strategy 1 -b_sensitivity 10 -me_range 128 -mpv_flags mv0+skip_rd+naq -mv0_threshold 0 -mbd 2 -mbcmp 2 -precmp 2 -subcmp 2 -cmp 2 -dia_size 2 -pre_dia_size 2 -preme 2 -last_pred 3 -dc 8 -lmin 1.0 -qmin 1 -i_qoffset 1.0 -qcomp 0.60 -an -f rawvideo -y NUL ffmpeg -i "C:\input.avs" -threads 2 -pass 2 -passlogfile Paso1.log -vcodec mpeg2video -b:v 2700000 -maxrate 9800000 -bufsize 1835000 -g 12 -bf 2 -sc_threshold -25000 -b_sensitivity 10 -me_range 128 -trellis 2 -mpv_flags cbp_rd+mv0+skip_rd+naq -mv0_threshold 0 -mbd 2 -mbcmp 2 -precmp 2 -subcmp 2 -cmp 2 -dia_size 2 -pre_dia_size 2 -preme 2 -last_pred 3 -dc 8 -lmin 1.0 -qmin 1 -i_qoffset 1.0 -qcomp 0.60 -aspect 4:3 -psnr -y "C:\video.m2v" @pause Last edited by GMJCZP; 6th June 2017 at 21:06. Reason: Added the parameter -g 12 (for video input of 23.976 fps only) |
4th June 2017, 04:20 | #11 | Link | |
Registered User
Join Date: Sep 2003
Location: Berlin, Germany
Posts: 3,105
|
Hi GMJCZP,
I am really no FFmpeg expert, I got all these advanced parameters from other users... You really use a negative value for sc_threshold? Quote:
I made a few short test conversions using your parameters and mine, but I could not see too many differences (my script was slower). Whatever, as usual you should use whatever works best for you, do some comparisons for sources you know well... Cheers manolito |
|
5th June 2017, 01:04 | #12 | Link |
Registered User
Join Date: Apr 2010
Location: I have a statue in Hakodate, Japan
Posts: 754
|
Thanks for answering me, manolito.
Using negative values of sc_threshold increases the appearance of I-frames, which appear mainly in scenes of high movement, while avoiding the macroblocks. In summary my configuration focuses on two aspects: - Avoid macroblocks (IMHO weak point of FFMPEG, scenes of high movement, that's why I like for low bitrates (<2500) to use HCEncoder) - Encode with "reasonable speed". Thanks again. Edit: When I use negative of sc_threshold values I'm actually mimicking the performance of HCEncoder. |
5th June 2017, 13:14 | #13 | Link |
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 11,020
|
Nifty little trick GMJCZP, nice one
__________________
I sometimes post sober. StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace "Some infinities are bigger than other infinities", but how many of them are infinitely bigger ??? |
18th June 2017, 13:51 | #14 | Link |
Registered User
Join Date: Sep 2003
Location: Berlin, Germany
Posts: 3,105
|
And here comes the competition using Fishman's parameters:
http://www102.zippyshare.com/v/ivWYugog/file.html Cheers manolito |
19th June 2017, 03:23 | #15 | Link |
Registered User
Join Date: Apr 2010
Location: I have a statue in Hakodate, Japan
Posts: 754
|
I was trying the Fishman script which uses the Fox New matrix and does not throw visual flaws like my script. Although in itself is not relevant in the performance of FFMPEG I have to find out which or which parameters cause the problem.
Last edited by GMJCZP; 19th June 2017 at 03:25. |
19th June 2017, 05:23 | #16 | Link |
Registered User
Join Date: Apr 2010
Location: I have a statue in Hakodate, Japan
Posts: 754
|
Version 3.1 (19/06/2017):
- Removed the trellis parameter that caused visual artifacts when using the FOX New matrix (now its default value is 0). As a consequence of this the coding speed in the second pass has been increased. - The FOX New matrix now suggests its use for bitrates greater than or equal to 1800k. |
21st June 2017, 17:43 | #17 | Link |
Registered User
Join Date: Apr 2010
Location: I have a statue in Hakodate, Japan
Posts: 754
|
Version 3.2 (21/06/2017):
- Disabled for the first pass several parameters, so the encoding speed has been increased drastically. - Added variable E_FQ (q) (Fishman0919 suggestion). - Changed the following default values: E_MLM = 0.75 (lmin) E_MBL = 50.0 (mblmin) E_RME = 0 (me_range) - Other minor changes. Last edited by GMJCZP; 21st June 2017 at 17:48. |
30th June 2017, 14:59 | #18 | Link |
Registered User
Join Date: Apr 2010
Location: I have a statue in Hakodate, Japan
Posts: 754
|
Version 3.3 (30/06/2017):
- Added variable E_DM (dark_mask) (Encoding enhancement in dark scenes). - Other minor changes. |
10th September 2019, 08:00 | #20 | Link |
Registered User
Join Date: Jun 2011
Location: Melbourne, Australia
Posts: 90
|
When using the -sc_factor in with these options, I get this error:
Unrecognised option 'sc_factor'. Error splitting the argument list: Option not found Also, if using the -me_method option, I get this error: Codec AVOption me_method (Set motion estimation method) specified for output file #0 (out.mpg) has not been used for any stream. The most likely reason is either wrong type (e.g. a video option with no video streams) or that it is a private option of some encoder which was not actually used for any stream. |
Thread Tools | Search this Thread |
Display Modes | |
|
|