Converting FLV files to MP4 via Post Processing (Orientation Post Processor)

Red5 Pro records files as FLV, but many people have asked about how to convert those files easily to MP4 so that more media players can be used to view them. The Red5 Pro OrientationPostProcessor uses FFMpeg to transcode a supplied FLV into an MP4 with all the recorded orientation events applied to the final video output. This allows the media to be played back without needing to handle orientation metadata. The processor may be used in combination with the S3UploaderPostProcessor to upload converted media once completed; the MP4 will be uploaded to the cloud alongside the FLV. Note that it does take time to convert to MP4, depending on the length of your recording, so the MP4 version will not be immediately available.

FFMpeg

The Transcoder uses two native applications: FFMpeg and an h.264 encoder, either x264 or OpenH264.

Having FFMpeg with h.264 support as a dependency carries licensing information along with its use / deployment. There are two primary licenses we are concerned with herein, GNU General Public License and GNU Lesser General Public License. GPL being the most restrictive of the two and requiring the implementer to meet their specific requirements (see the license and/or consult your lawyer).

Installing FFMpeg v3.3.2 via apt-get

From How to Install FFmpeg 3.3. in Ubuntu 16.04, 14.04.

In order to use the new OrientationPostProcessor method, you must have at least version 3.3 of FFMpeg. If you have an older version of FFMpeg installed, remove that first:

sudo apt-get remove ffmpeg

Then, add the jonathonf ppa:

sudo add-apt-repository ppa:jonathonf/ffmpeg-3

And confirm the resulting message by pressing [enter].

If you are on Ubuntu 14.04, you will also need to run:

sudo add-apt-repository ppa:jonathonf/tesseract

Next:

sudo apt update && sudo apt upgrade

Now FFmpeg is available to be installed with apt:

sudo apt-get install ffmpeg

Server Configuration

The writer post processor used by FLVWriter is configured in the server's common configuration file: (red5pro)/conf/red5-common.xml. The example below displays the configuration with the TranscodePostProcessor (note - this bean exists in the file, you just need to modify & add the writer post processor value):

<bean id="flv.impl" class="org.red5.io.flv.impl.FLV">
  <property name="cache">
    <ref bean="object.cache"/>
  </property>
  <property name="writerPostProcessors">
    <set>
      <value>com.red5pro.media.processor.OrientationPostProcessor</value>
    </set>
  </property>
</bean>

This configuration indicates the transcoder writerPostProcessor which implements IPostProcessor will be executed after a file is finalized by the FLVWriter.

If you choose to use both the S3UploaderPostProcessor and the OrientationPostProcessor methods, then the bean should look like:

<bean id="flv.impl" class="org.red5.io.flv.impl.FLV">
  <property name="cache">
    <ref bean="object.cache"/>
  </property>
  <property name="writerPostProcessors">
    <set>
      <value>com.red5pro.media.processor.OrientationPostProcessor</value>
      <value>com.red5pro.media.processor.S3UploaderPostProcessor</value>
    </set>
  </property>
</bean>

Additional FFMpeg compilation detail may be found here: Ubuntu FFMpeg Compilation Guide

Compiling FFMpeg with OpenH264

Alternatively to installing via apt-get, you can compile FFMpeg with OpenH264.

  • Get the FFMpeg source via git: git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
  • Change directory to ffmpeg: cd ffmpeg
  • Make a build directory inside the ffmpeg directory: mk ffmpeg_build
  • Go back to the top-level: cd ..
  • Get the OpenH264 (Version 1.5) source code (download as zip)
  • Unzip openh264v1.5.zip into openh264 directory
  • Enter the openh264 directory: cd openh264
  • Modify the openh264/Makefile changing PREFIX to point at the full path to your ffmpeg/ffmpeg_build directory; Example: PREFIX=/workspace/infrared5/booma/pov-booma-server-side/ffmpeg/ffmpeg_build
  • Save the Makefile
  • Execute make install
  • Enter the ffmpeg directory, execute the build script below
#!/bin/bash
# Red5 Pro FFMpeg build script for Transcoding

PKG_CONFIG_PATH="$(pwd)/ffmpeg_build/lib/pkgconfig"
export PKG_CONFIG_PATH

function build_ff
{
./configure \
    --disable-everything \
    --disable-doc --disable-ffplay --disable-ffprobe --disable-ffserver \
    --enable-libopenh264 \
    --disable-avdevice \
    --disable-programs \
    --enable-ffmpeg \
    --disable-indevs --disable-outdevs \
    --disable-swscale-alpha \
    --disable-symver --disable-iconv \
    --disable-bsfs --disable-videotoolbox \
    --disable-protocols \
    --enable-protocol=file \
    --enable-protocol=rtmp \
    --disable-decoders \
    --enable-decoder=aac \
    --enable-decoder=h264 \
    --enable-decoder=h263 \
    --enable-decoder=flv \
    --enable-decoder=nellymoser \
    --disable-encoders \
    --enable-encoder=aac \
    --enable-encoder=libopenh264 \
    --disable-filters \
    --enable-filter=aresample \
    --disable-parsers \
    --enable-parser=aac \
    --enable-parser=h264 \
    --disable-demuxers \
    --enable-demuxer=flv \
    --disable-muxers \
    --enable-muxer=flv \
    --enable-muxer=mp4 \
    --disable-shared \
    --enable-static \
    --enable-small \
    --extra-cflags="-Os -fpic $ADDI_CFLAGS -I$(pwd)/ffmpeg_build/include" \
    --extra-ldflags="$ADDI_LDFLAGS -L$(pwd)/ffmpeg_build/lib" \
    --pkg-config-flags="--static" \
    --disable-debug --prefix=$(pwd)/ffmpeg_build \
make && make install
}
build_ff

Lastly, copy the ffmpeg executable from ffmpeg_build to a known location which will be used in your conf/cloudstorage-plugin.properties file as the value for the ffmpeg.path entry. Note: the cloudstorage-plugin.properties file has GPL (x264) uncommented by default. If you are using OpenH264, then you need to uncomment the LGPL command template:

#ffmpeg.template=%s -y -report -async 1 -i %s -acodec aac -b:a 128k -ar 44100 -vcodec libopenh264 -pix_fmt yuv420p -profile:v baseline -level 3.0 %s