How to use FFmpeg with Wowza Streaming Engine (MPEG-TS)

FFmpeg is a command line tool used to convert multimedia files between various formats. This article describes how to use FFmpeg as a live video/audio encoder with Wowza Streaming Engine™ media server software.

Note: FFmpeg version N-52458-gaa96439 (04-30-2013) or later is required. FFmpeg commands can change frequently with new releases so always check with the official FFmpeg documentation for the latest encoding commands.

Contents


Basic FFmpeg command notation
Setting up re-streaming using FFmpeg
Transcoding streams with FFmpeg Other FFmpeg command line examples More resources

Basic FFmpeg command notation


A basic ffmpeg command has the following form:
ffmpeg [input-options] -i [input-file] [output-options] [output-stream-URI]
Where:
 
  • [input-options] are any options to apply to the input or source. For example, if the source is a video-on-demand file, prefix the file name with the -re option to limit reading the input (and therefore writing the output) to the encoded bitrate.
     
  • [input-file] is the video-on-demand file or live application stream URL to re-stream.
     
  • [output-options] are any options to apply to the output or destination. For example, use the -f option to set the output container format.
     
  • [output-stream-URI] is the destination stream URI. The format of this URI depends on the output container format.

For more detailed information about using FFmpeg, see the official FFmpeg documentation.

Setting up re-streaming using FFmpeg


This example re-streams the [install-dir]/content/sample.mp4 video file included in your Wowza media server installation (for Windows operating systems).

To install and set up FFmpeg (Windows):
 
  1. Download the most recent build of FFmpeg for your platform from http://ffmpeg.zeranoe.com/builds// and unzip the package contents on the same computer that's running the Wowza media server.
     
  2. Create the following directory structure in the C: drive: C:/usr/local/share/ffmpeg. Copy the contents of the presets folder in the FFmpeg package into this new directory.
     
  3. Open a Command Prompt window and enter the following command to change the current directory to the FFmpeg bin folder:
     
    cd [ffmpeg-install-dir]\bin
  4. Enter the following FFmpeg command to re-stream sample.mp4 from [install-dir]/content:
    ffmpeg -re -i "%WMSAPP_HOME%/content/sample.mp4"  -vcodec libx264  -vb 150000 -g 60 -vprofile baseline -level 2.1 -acodec aac -ab 64000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb -strict experimental -f mpegts udp://127.0.0.1:10000?pkt_size=1316
  5. Follow the instructions in the How to publish and play a live stream (MPEG-TS based encoder) tutorial to set up a live or on-demand application to publish the FFmpeg stream. When you're setting up the new stream file, use udp://127.0.0.1:10000?pkt_size=1316 as the Stream URI.
The FFmpeg commands used in the above example produce low bitrate, low complexity, low-quality streams that should be playable on most playback devices and systems. To produce a higher quality stream, use the following example:
ffmpeg -re -i "%WMSAPP_HOME%/content/sample.mp4"  -vcodec libx264  -vb 500000 -g 60 -vprofile main -acodec aac -ab 128000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb -strict experimental -f mpegts udp://127.0.0.1:10000?pkt_size=1316
Notes:
  • The sample.mp4 file is about six minutes long. FFmpeg will stop when streaming ends so you may have to restart FFmpeg several times during configuration and testing. To loop your command to generate a constant test stream, prefix the FFmpeg command with:
     
    FOR /L %% IN (0) DO
  • To host FFmpeg on a different computer than the server hosting the Wowza media server, change udp://127.0.0.1:10000 in the command line to the IP address of the server running the Wowza media server and any destination UDP port. Be sure that the specified UDP port is open on any routers or firewalls between the server hosting FFmpeg and the server running the Wowza media server.
     
  • Microsoft Silverlight players prefer a two-second keyframe frequency. To create a stream with a two-second keyframe frequency, set the -r (frame rate) option to half of the -g (GOP or i-frame interval) value.

Transcoding streams with FFmpeg


FFmpeg can be used to transcode an RTSP, native RTP, or MPEG-TS streams. To transcode a video source, change the first argument of the command line (the path to the sample.mp4 file) to the desired source and remove the -re command line option. Here are a few examples using each format:

RTSP/RTP camera

ffmpeg -i "rtsp://[camera-ip-address]/[camera-URI-syntax]" -vcodec libx264 -vb 150000 -g 60 -vprofile baseline -level 2.1 -acodec aac -ab 64000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb -strict experimental -f mpegts udp://127.0.0.1:10000?pkt_size=1316

MPEG-TS stream

ffmpeg -i "udp://localhost:[port]" -vcodec libx264 -vb 150000 -g 60 -vprofile baseline -level 2.1 -acodec aac -ab 64000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb -strict experimental -f mpegts udp://127.0.0.1:10000?pkt_size=1316

Native RTP stream

ffmpeg -i "unicast.sdp" -vcodec libx264 -vb 150000 -g 60 -vprofile baseline -level 2.1 -acodec aac -ab 64000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb -strict experimental -f mpegts udp://127.0.0.1:10000?pkt_size=1316

Streaming alternative outgoing protocols

FFmpeg can publish streams using other outgoing protocols such as RTMP and RTSP/RTP (QuickTime ANNOUNCE).

RTMP

To send a stream using RTMP, change the output portion of the FFmpeg URL from:
 

-f mpegts udp://127.0.0.1:10000?pkt_size=1316

To:
 
-f flv rtmp://127.0.0.1/live/myStream


The RTMP URL must follow this format: rtmp://[wowza-ip-address]:1935/[application]/[streamName].

RTSP/RTP

To send a stream using RTSP/RTP, change the output portion of the FFmpeg URL from:
 

-f mpegts udp://127.0.0.1:10000?pkt_size=1316

To:
 
-f rtsp rtsp://127.0.0.1:1935/live/myStream.sdp

The RTSP URL must follow this format: rtsp://[wowza-ip-address]:1935/[application]/[streamName].

Where:
 
  • The [wowza-ip-address] placeholder is the IP address of your Wowza media server.
     
  • The [application] placeholder is the name of your Wowza media server application (such as live or vod).
     
  • The [stream-name] placeholder is the name of your stream in that application.

Other FFmpeg command line examples


Re-stream RTSP with password authentication

This example uses security credentials, H.264/AAC, baseline profile, level 3.0, 24 fps (frame rate), two-second keyframe frequency, 350kbps stream, 44.1khz, stereo.
ffmpeg -re -i sample.mp4 -c:v libx264 -profile:v baseline -level 3.0 -r 24 -g 48 -keyint_min 48 -sc_threshold 0 -vb 310k -c:a libvo_aacenc -ab 40k -ar 44100 -ac 2 -f rtsp -muxdelay 0.1 rtsp://username:password@[wowza-ip-address]:1935/live/myStream

Re-stream RTP with SDP

This example captures local hardware devices and generates SDP file in the current directory and uses a 15 fps frame rate and 12bit color:
ffmpeg -y -f dshow -s 640x480 -r:v 15 -i video="Integrated Webcam":audio="Microphone (Realtek High Defini" -c:v libx264 -pix_fmt yuv420p -vb 450 -an -map 0:0 -f rtp rtp://127.0.0.1:10000 -flags +global_header -c:a libvo_aacenc -vn -map 0:1 -f rtp rtp://127.0.0.1:10002 > 10000.sdp

Show stream encoding parameters

This example shows VOD or live stream encoding properties for troubleshooting purposes:
 
ffprobe -show_streams [stream-name]

The following is the output generated by this command when executed on sample.mp4:
[STREAM]
index=0
codec_name=h264
codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
profile=Constrained Baseline
codec_type=video
codec_time_base=1/60
codec_tag_string=avc1
codec_tag=0x31637661
width=512
height=288
has_b_frames=0
sample_aspect_ratio=1:1
display_aspect_ratio=16:9
pix_fmt=yuv420p
level=21
timecode=N/A
id=N/A
r_frame_rate=30/1
avg_frame_rate=30/1
time_base=1/15360
start_pts=0
start_time=0.000000
duration_ts=9747968
duration=634.633333
bit_rate=640158
nb_frames=19039
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=0
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
TAG:language=und
TAG:handler_name=VideoHandler
[/STREAM]
[STREAM]
index=1
codec_name=aac
codec_long_name=AAC (Advanced Audio Coding)
profile=unknown
codec_type=audio
codec_time_base=1/48000
codec_tag_string=mp4a
codec_tag=0x6134706d
sample_fmt=fltp
sample_rate=48000
channels=2
bits_per_sample=0
id=N/A
r_frame_rate=0/0
avg_frame_rate=0/0
time_base=1/48000
start_pts=-1024
start_time=-0.021333
duration_ts=30439936
duration=634.165333
bit_rate=96193
nb_frames=29727
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=0
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
TAG:language=und
TAG:handler_name=SoundHandler
[/STREAM]

List available devices

This example lists available hardware device names such as webcams and microphones for use as FFmpeg sources:
 
ffmpeg -list_devices true -f dshow -i dummy

Convert .ts source files to .mp4

This example converts .ts file (or any FFmpeg compatible sources) to the .mp4 container format for use with Wowza video-on-demand playback:
 
ffmpeg -i input.ts -c:v copy -c:a copy output.mp4

Remove edit lists from .mp4 files

This example removes the edit lists from .mp4 files when the audio and video are out of sync. Some video recorders use edit lists to synchronize audio and video in an MP4 or MOV container when the audio and video start at different timecodes, but Wowza Streaming Engine doesn't use the edit lists. Instead, the following FFmpeg commands create separate audio and video renditions of the file in an MKV format and remove a specified number of seconds, in this case 4, from the beginning of the file. Then, the video and audio files are remuxed back together to create an .mp4 file that is, in this example, 5 seconds shorter. Adjust the number of seconds removed from the file as needed to synchronize your audio and video.
ffmpeg -y -ss 4 -i source.mp4 -c:v copy -an source_video.mkv
ffmpeg -y -ss 4 -i source.mp4 -vn -c:a copy source_audio.mkv
ffmpeg -y -i source_video.mkv -i source_audio.mkv -c:v copy -c:a copy source_trim.mp4

Check keyframe alignment (Linux only)

This example checks video-on-demand files for proper keyframe alignment. For this example, ensure the PTS and/or DTS timestamps are identical for each corresponding I-frame for each file in your multi-bitrate stream. For more information on keyframe alignment in adaptive bitrate streaming, see How to do adaptive bitrate streaming.
 
ffprobe -output_format csv -show_frames [stream-name] |grep I

Generate keyframe-aligned video files (Linux only)

This example generates keyframe-aligned, multi-bitrate video files to use for on-demand adaptive bitrate streaming. The commands use the bigbuckbunny_1500.mp4 video file that you can download from the Wowza website and use for testing purposes.
ffmpeg -y -i bigbuckbunny_1500.mp4 -c:a libfaac -ac 2 -ab 64k -c:v libx264 -preset:v veryfast -threads 0 -r 24 -g 48 -keyint_min 48 -sc_threshold 0 -x264opts no-mbtree:bframes=1 -pass 1 -b:v 286k -s 384x216 bigbuck-350k.mp4
ffmpeg -y -i bigbuckbunny_1500.mp4 -c:a libfaac -ac 2 -ab 64k -c:v libx264 -preset:v fast -threads 0 -r 24 -g 48 -keyint_min 48 -sc_threshold 0 -x264opts no-mbtree:bframes=1 -pass 2 -b:v 286k -s 384x216 bigbuck-350k.mp4
ffmpeg -y -i bigbuckbunny_1500.mp4 -c:a libfaac -ac 2 -ab 64k -c:v libx264 -preset:v fast -threads 0 -r 24 -g 48 -keyint_min 48 -sc_threshold 0 -x264opts no-mbtree:bframes=1 -pass 2 -b:v 836k -s 640x360 bigbuck-900k.mp4

More resources



Originally Published: 11-22-2010.
Updated: For Wowza Streaming Engine on 07-16-2015.
 

If you're having problems or want to discuss this article, post in our forum.