How do I convert videos to H.265/HEVC format in Ubuntu?

I see that H.265/HEVC encoding is gathering momentum but under some versions of Ubuntu it is not all that easy to produce H.265 video streams.

How should I go about using x265, in a manner that integrates with Ubuntu (especially the LTS releases Trusty and Xenial), to produce HEVC video files under Ubuntu?

References:

  1. x265 HEVC Encoder: The commandline encoder
  2. x265: Videolan site

Solution 1:

The best solution so far on my system has been:

  1. For Trusty Tahr 14.04 LTS: use an up to date FFmpeg and the most recent x265, which necessitates some compiling and subsequent packaging
  2. For Xenial Xerus 16.04 LTS: simply run:

    sudo apt-get install ffmpeg  libavcodec-extra
    

With either of the previous methods then use the following:

ffmpeg -i input \
   -c:v libx265 -preset slow -x265-params crf=22 \
   -c:a libmp3lame -b:a 128k \
   output.mp4

Note that this creates an mp3 audio stream as well as an h.265 video stream. To create an aac stream the line -c:a libmp3lame -b:a 128k could be replaced with the following:

-c:a aac -strict experimental -b:a 128k

The -strict experimental option will not be required if your copy of FFmpeg was released after December 2015 when development of the native aac encoder matured. Bear in mind that this option will still be required for the repository FFmpeg for Xenial Xerus 16.04 LTS.

References:

  • FFmpeg trac: FFmpeg and H.265 Encoding Guide
  • FFmpeg git: aacenc: remove the experimental flag

Solution 2:

Method 01

You can install Internet friendly media encoder:

enter image description here

Run these commands in your Terminal:

sudo add-apt-repository -y ppa:upubuntu-com/multimedia  
sudo apt-get update  
sudo apt-get install ifme

Now you can open the program ifme from Dash.

source

Method 02

Once you compiled the program following the instructions in here or here, first you have to encode the video into YUV format:

avconv -i MyVideo.mp4 MyVideo.yuv

Then you can convert YUV video into x265 format:

./x265 --input-res 640x360 --fps 24000/100 MyVideo.yuv -o MyVideo.h265

Solution 3:

This script worked for me:

ffmpeg -i input_file.mpg -pix_fmt yuv420p -f yuv4mpegpipe - |\
     x265-10bit --profile main10 --preset slower --crf 20 --input - --y4m -o output_file.mpg