How to Stream IP Camera to RTMP with FFmpeg
This guide explains how to stream video from an IP camera to an RTMP server using FFmpeg, a powerful open-source command-line tool for handling multimedia. You will learn how to capture the camera’s RTSP stream, format the data correctly, and transmit it to RTMP destinations such as YouTube, Twitch, or private media servers.
Prerequisites
Before starting, ensure you have the following information and tools:
* FFmpeg installed on your system. * The RTSP
URL of your IP camera (usually formatted as
rtsp://username:password@camera_ip:port/stream_path). * The
RTMP Server URL and Stream Key of your
destination.
The Basic Command (No Re-encoding)
If your IP camera already outputs video in H.264 format and audio in AAC format, you can stream without re-encoding. This saves CPU resources because FFmpeg simply copies the stream.
Run the following command in your terminal:
ffmpeg -rtsp_transport tcp -i "rtsp://username:password@camera_ip:554/live" -c:v copy -c:a copy -f flv "rtmp://your_rtmp_server/live/stream_key"Command Breakdown:
-rtsp_transport tcp: Forces FFmpeg to use TCP instead of UDP for the RTSP connection, which prevents packet loss and video corruption.-i "rtsp://...": Specifies the input source (your IP camera’s RTSP stream).-c:v copy: Copies the video codec directly without re-encoding.-c:a copy: Copies the audio codec directly.-f flv: Forces the output format to FLV, which is required for RTMP streaming."rtmp://...": The destination RTMP URL combined with your unique stream key.
The Advanced Command (With Re-encoding)
If your camera outputs in a format incompatible with RTMP (like H.265), or if you need to compress the video to fit bandwidth limitations, you must transcode the stream.
Use this command to re-encode the video to H.264 and audio to AAC:
ffmpeg -rtsp_transport tcp -i "rtsp://username:password@camera_ip:554/live" -c:v libx264 -preset veryfast -b:v 2000k -maxrate 2000k -bufsize 4000k -g 60 -c:a aac -b:a 128k -f flv "rtmp://your_rtmp_server/live/stream_key"Command Breakdown for Re-encoding:
-c:v libx264: Encodes the video to H.264.-preset veryfast: Optimizes encoding speed to reduce latency and CPU usage.-b:v 2000k: Sets the target video bitrate to 2000 Kbps.-maxrate 2000kand-bufsize 4000k: Limits maximum bitrate to ensure stream stability over fluctuating internet connections.-g 60: Sets the Group of Pictures (GOP) size. For a 30fps stream, a GOP of 60 forces a keyframe every 2 seconds, which is required by most RTMP ingest servers (like YouTube).-c:a aac: Converts the audio to AAC format.-b:a 128k: Sets the audio bitrate to 128 Kbps.
Troubleshooting Common Issues
- Green Screen or Artifacts: If you experience video
corruption, ensure
-rtsp_transport tcpis placed before the-iparameter to prevent packet loss. - Connection Dropping: If the stream disconnects
frequently, check your upload bandwidth and lower the bitrate using the
-b:vflag. - No Audio: If your camera does not have a
microphone, replace
-c:a copyor-c:a aacwith-anto completely disable audio in the output stream.