Thursday 29 November 2018

Converting video from 1080p to 720p with smallest quality loss using ffmpeg


I stack with my high quality movie: 1080p with 60fps. The trouble is lags while playing.


I tried to convert it to 720p with ffmpeg:


ffmpeg -i MyMovie.mkv -r 60 -s hd720 MyMovie_720p.mkv

But I there is significant quality loss because bit rate decreased from 32.3 Mbps to 2.8 Mbps. How can I specify right bit rate for video stream, and left all other stream copied pristine?


Input file mediainfo:


ID                                       : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L4.2
Format settings, CABAC : Yes
Format settings, ReFrames : 4 frames
Codec ID : V_MPEG4/ISO/AVC
Duration : 2h 58mn
Bit rate : 32.3 Mbps
Width : 1 920 pixels
Height : 1 080 pixels
Display aspect ratio : 16:9
Frame rate : 59.940 fps
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.260
Stream size : 40.2 GiB (92%)
Writing library : x264 core 116 r2019 9cc407d
Encoding settings : cabac=1 / ref=4 / deblock=1:-2:-1 / analyse=0x3:0x113 / me=umh / subme=10 / psy=1 / psy_rd=0.80:0.20 / mixed_ref=1 / me_range=32 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=0 / chroma_qp_offset=-3 / threads=6 / sliced_threads=0 / nr=0 / decimate=0 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=6 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=60 / rc=crf / mbtree=0 / crf=14.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / vbv_maxrate=50000 / vbv_bufsize=62500 / crf_max=0.0 / nal_hrd=none / ip_ratio=1.40 / pb_ratio=1.30 / aq=1:0.60
Language : English
Default : Yes
Forced : No
Matrix coefficients

: BT.709-5, BT.1361, IEC 61966-2-4 709, SMPTE RP177

Answer



Here is an example which should give you the highest quality video (I'm not speaking about the resulting file size). Note that this video might not be playable on all devices or players:


ffmpeg -i MyMovie.mkv -vf scale=-1:720 -c:v libx264 -crf 0 -preset veryslow -c:a copy MyMovie_720p.mkv

To get a "visually lossless" quality, you can use:


ffmpeg -i MyMovie.mkv -vf scale=-1:720 -c:v libx264 -crf 18 -preset veryslow -c:a copy MyMovie_720p.mkv

Now let's see what we have here:


The scale video filter is for resizing the video. You just set one size – which is the height in this example – and use -1 for the other dimension. ffmpeg will recalculate the correct value automatically while preserving the aspect ratio.


Quality controlled with the -crf option:



The range of the quantizer scale is 0-51: where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28. Consider 18 to be visually lossless or nearly so: it should look the same or nearly the same as the input but it isn't technically lossless.


The range is exponential, so increasing the CRF value +6 is roughly half the bitrate while -6 is roughly twice the bitrate. General usage is to choose the highest CRF value that still provides an acceptable quality. If the output looks good, then try a higher value and if it looks bad then choose a lower value.



You can find more info in the x264 encoding guide.


You control the tradeoff between video encoding speed and compression efficiency with the -preset options. Those are ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow. Default is medium. The veryslow option offers the best compression efficiency (resulting in a smaller file size for the same quality) but it is very slow – as the name says.


The audio will be stream copied directly from the input file to the output file without any changes.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...