FFmpeg - Video streaming configuration tutorial

Use FFmpeg As Encoder for Video Streaming

Follow this guide to configure FFmpeg as a live video encoder and stream directly to your Primcast video server over RTMP. All configuration is done via command line.

Command line guide RTMP streaming 24/7 live support

Step-by-step guide

Each step explains one part of the FFmpeg command. All values shown in UPPERCASE must be replaced with the correct details from your Primcast account.

1

The full FFmpeg command

This is the complete example command. Copy it and replace the placeholders with your account details. The sections below explain each flag.

ffmpeg -i 'FILE or LINK' \
  -crf 30 -preset ultrafast \
  -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k \
  -vcodec libx264 -r 25 -b:v 300k \
  -f flv \
  'rtmp://USERNAME:PASSWORD@STREAMING_ADDRESS:1935/APPLICATION_NAME/livestream'
2

Configure your input source (-i)

The -i flag tells FFmpeg where to pull the input stream from. It can be a file, a capture device, or another stream:

  • Device: -i video="screen-capture-recorder":audio="Stereo Mix (IDT High Definition)"
  • File: -i '/home/user/sample.mp4'
  • Stream: -i 'udp://127.0.0.0:1000/'
3

Video quality and encoding flags

These flags control how the video is encoded:

  • -crf 30 - Sets the Constant Rate Factor. Keeps reasonably consistent video quality while varying bitrate during complex scenes. A value of 30 allows somewhat lower quality and bitrate.
  • -preset ultrafast - Provides the fastest possible encoding speed.
  • -vcodec libx264 - Sets the video codec to H.264.
  • -r 25 - Sets the frame rate to 25 fps.
  • -b:v 300k - Sets the video bitrate to 300 kbps.
4

Audio encoding flags

These flags control how the audio is encoded:

  • -acodec aac - Sets the audio codec to AAC (internal AAC encoder).
  • -strict experimental - Allows use of experimental codecs. Required because the internal AAC encoder is experimental.
  • -ar 44100 - Sets the audio sample rate to 44,100 Hz.
  • -ac 2 - Specifies two channels of audio (stereo).
  • -b:a 96k - Sets the audio bitrate to 96 kbps.
5

Output format and RTMP destination

-f flv wraps the output stream in an FLV container, which is required for RTMP.

The final argument is the RTMP destination URL. Replace each placeholder with your account details:

rtmp://USERNAME:PASSWORD@STREAMING_ADDRESS:1935/APPLICATION_NAME/livestream
  • Replace USERNAME and PASSWORD with the credentials from your Primcast account.
  • Replace STREAMING_ADDRESS with the streaming address from your account dashboard (e.g. test.primcast.com).
  • Replace APPLICATION_NAME with the application name shown on your dashboard.

Once streaming, you can view the output via the HLS link in a player like VLC, or embed it using the Generate Player link in your dashboard.

FAQ

Common questions about using FFmpeg for live video streaming with Primcast.

Where do I find my streaming address, username, and password?

Your streaming address, username, password, and application name are all available in your Primcast account dashboard. You can also find them in your activation email.

Can I stream from a file instead of a live source?

Yes. Use the -i '/path/to/file.mp4' flag to stream from a local file. FFmpeg will encode and push it to your RTMP server as if it were a live stream.

What video and audio codecs does Primcast require?

Primcast video servers accept H.264 video (libx264) and AAC audio (aac) delivered in an FLV container over RTMP. These are the defaults in the example command.

What bitrate should I use?

The example uses 300 kbps for video (-b:v 300k) and 96 kbps for audio (-b:a 96k). You can increase the video bitrate for better quality if your upload bandwidth allows it. Most streams work well between 500 kbps and 2500 kbps for video.

How do I view my stream once it is live?

Once FFmpeg is streaming, you can watch it using the HLS link from your Primcast dashboard in a player like VLC. You can also generate an embeddable player from the Generate Player link in your account.

Ready to go live?

Our audio and video streaming services include unlimited connections, unlimited bandwidth, and 24/7 expert support to keep you on air.