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:


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:


Troubleshooting Common Issues