Configure FFmpeg RTSP User-Agent and Credentials

This article provides a straightforward guide on how to configure custom user-agents and authentication credentials in an RTSP URL using FFmpeg. You will learn the correct command-line syntax to pass usernames, passwords, and custom header identifiers to successfully connect to secure IP cameras and RTSP streaming servers.

To configure authorization credentials and a custom user-agent in FFmpeg, you must specify the credentials within the RTSP URL itself and use the -user-agent input option before defining your input stream.

Setting Authorization Credentials

FFmpeg accepts RTSP authentication credentials embedded directly within the connection URL. The standard syntax for passing a username and password is:

rtsp://username:password@hostname:port/path

For example, if your username is admin, your password is SecretPassword123, and the camera IP is 192.168.1.50, the input URL will look like this:

rtsp://admin:SecretPassword123@192.168.1.50:554/stream1

Important Note on Special Characters: If your password contains special characters (such as @, :, /, or ?), you must URL-encode them. For example, if your password is P@ssword, the @ symbol must be replaced with %40, resulting in P%40ssword.

Setting the User-Agent

Many RTSP servers and IP cameras restrict access or change behavior based on the connecting client’s user-agent. You can override the default FFmpeg user-agent by using the -user-agent parameter.

This parameter must be placed before the input flag (-i) in your command line sequence so that it applies to the incoming connection initialization.

ffmpeg -user-agent "YourCustomUserAgent/1.0" -i rtsp://username:password@hostname:port/path

Complete Command Example

Below is a complete command that combines both the custom user-agent and RTSP authentication to stream or save a video feed. This example also includes the -rtsp_transport tcp flag, which is highly recommended for RTSP connections to prevent packet loss:

ffmpeg -user-agent "SecurityClient/2.0" -rtsp_transport tcp -i rtsp://admin:SecretPassword123@192.168.1.50:554/stream1 -c copy output.mp4

In this command: * -user-agent "SecurityClient/2.0" sets the custom user-agent header. * -rtsp_transport tcp forces FFmpeg to use TCP instead of UDP for a more stable RTSP connection. * -i rtsp://admin:SecretPassword... provides the authenticated input source. * -c copy copies the video and audio streams directly without re-encoding, saving CPU resources. * output.mp4 is the destination file.