adminze

Mar 132019
 

Recently I noticed that the h265 encoded videos on my mobile phone seem very large. I re-encoded them using ffmpeg and found the filesize to be 90% smaller (in average 75% smaller)! Audio was fine. Note that the re-encoding took a while – about 7-10 seconds encoding for 1 second of video.

This command can be used to re-encode the videos (and copy the original audio stream without re-encoding):

for i in $(ls *mp4) ; do nice ffmpeg -i $i -map_metadata 0 -map_chapters 0 -c:v libx265 -q:v 1 -c:a copy -preset medium h265_$i ; done

Sample output:

[..]

built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
configuration: –prefix=/usr –extra-version=’1~deb9u1′ –toolchain=hardened –libdir=/usr/lib/x86_64-linux-gnu –incdir=/usr/include/x86_64-linux-gnu –enable-gpl –disable-stripping –enable-avresample –enable-avisynth –enable-gnutls –enable-ladspa –enable-libass –enable-libbluray –enable-libbs2b –enable-libcaca –enable-libcdio –enable-libebur128 –enable-libflite –enable-libfontconfig –enable-libfreetype –enable-libfribidi –enable-libgme –enable-libgsm –enable-libmp3lame –enable-libopenjpeg –enable-libopenmpt –enable-libopus –enable-libpulse –enable-librubberband –enable-libshine –enable-libsnappy –enable-libsoxr –enable-libspeex –enable-libssh –enable-libtheora –enable-libtwolame –enable-libvorbis –enable-libvpx –enable-libwavpack –enable-libwebp –enable-libx265 –enable-libxvid –enable-libzmq –enable-libzvbi –enable-omx –enable-openal –enable-opengl –enable-sdl2 –enable-libdc1394 –enable-libiec61883 –enable-chromaprint –enable-frei0r –enable-libopencv –enable-libx264 –enable-shared
libavutil 55. 34.101 / 55. 34.101
libavcodec 57. 64.101 / 57. 64.101
libavformat 57. 56.101 / 57. 56.101
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libavresample 3. 1. 0 / 3. 1. 0
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from ‘VID_20181216_182703.mp4’:
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
creation_time : 2018-12-16T17:27:32.000000Z
location : +50.1604+008.6456/
location-eng : +50.1604+008.6456/
com.android.version: 8.1.0
Duration: 00:00:27.51, start: 0.000000, bitrate: 20208 kb/s
Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuvj420p(pc, bt470bg/bt470bg/smpte170m), 1920×1080, 19898 kb/s, SAR 1:1 DAR 16:9, 29.74 fps, 120 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2018-12-16T17:27:32.000000Z
handler_name : VideoHandle
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 192 kb/s (default)
Metadata:
creation_time : 2018-12-16T17:27:32.000000Z
handler_name : SoundHandle
[swscaler @ 0x55c9ef70c200] deprecated pixel format used, make sure you did set range correctly
x265 [info]: HEVC encoder version 0.0
x265 [info]: build info [Linux][GCC 6.3.0][64 bit] 8bit+10bit+12bit
x265 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
x265 [info]: Main profile, Level-5 (Main tier)
x265 [info]: Thread pool created using 4 threads
x265 [info]: Slices : 1
x265 [info]: frame threads / pool features : 2 / wpp(17 rows)
x265 [info]: Coding QT: max CU size, min CU size : 64 / 8
x265 [info]: Residual QT: max TU size, max depth : 32 / 1 inter / 1 intra
x265 [info]: ME / range / subpel / merge : hex / 57 / 2 / 2
x265 [info]: Keyframe min / max / scenecut : 25 / 250 / 40
x265 [info]: Lookahead / bframes / badapt : 20 / 4 / 2
x265 [info]: b-pyramid / weightp / weightb : 1 / 1 / 0
x265 [info]: References / ref-limit cu / depth : 3 / on / on
x265 [info]: AQ: mode / str / qg-size / cu-tree : 1 / 1.0 / 32 / 1
x265 [info]: Rate Control / qCompress : CRF-28.0 / 0.60
x265 [info]: tools: rd=3 psy-rd=2.00 rskip signhide tmvp strong-intra-smoothing
x265 [info]: tools: lslices=6 deblock sao
Output #0, mp4, to ‘h265_VID_20181216_182703.mp4’:
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
creation_time : 2018-12-16T17:27:32.000000Z
location : +50.1604+008.6456/
location-eng : +50.1604+008.6456/
com.android.version: 8.1.0
encoder : Lavf57.56.101
Stream #0:0(eng): Video: hevc (libx265) ([35][0][0][0] / 0x0023), yuv420p, 1920×1080 [SAR 1:1 DAR 16:9], q=2-31, 120 fps, 15360 tbn, 120 tbc (default)
Metadata:
creation_time : 2018-12-16T17:27:32.000000Z
handler_name : VideoHandle
encoder : Lavc57.64.101 libx265
Stream #0:1(eng): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 192 kb/s (default)
Metadata:
creation_time : 2018-12-16T17:27:32.000000Z
handler_name : SoundHandle
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> hevc (libx265))
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 3301 fps= 11 q=-0.0 Lsize= 16891kB time=00:00:27.48 bitrate=5034.8kbits/s dup=2483 drop=0 speed=0.0902x
video:16180kB audio:645kB subtitle:0kB other streams:0kB global headers:1kB muxing overhead: 0.393501%
x265 [info]: frame I: 14, Avg QP:31.45 kb/s: 57839.52
x265 [info]: frame P: 858, Avg QP:35.14 kb/s: 14083.96
x265 [info]: frame B: 2429, Avg QP:39.71 kb/s: 1234.82
x265 [info]: Weighted P-Frames: Y:3.6% UV:3.6%
x265 [info]: consecutive B-frames: 10.0% 2.8% 34.2% 4.9% 48.2%

encoded 3301 frames in 304.74s (10.83 fps), 4814.65 kb/s, Avg QP:38.49