Introduction
FFmpeg can publish a live stream to CDNsun over RTMP. This guide focuses on the current FFmpeg publishing workflow for RTMP Push with CDN HTTP Live and CDN Live services.
Please note that Publishing Points also support HLS Pull, HLS Push, and RTMP Pull. This article focuses on RTMP Push because that is the Publishing Point mode used when FFmpeg sends a stream directly to CDNsun. If you are generating HLS output with FFmpeg instead of publishing RTMP, create the corresponding Publishing Point input method and follow that workflow instead.
Create the service and collect the required values
CDN HTTP Live service
First create a Publishing Point with RTMP Push input method. Then create a CDN HTTP Live service using that Publishing Point as its Origin Publishing Point.
On Services/How-To note the following value.
- Server URL - rtmp://push-1.cdnsun.com/p12345
CDN Live service
Create a CDN Live service with Push Publishing. On Services/How-To note the following values.
- Server URL - rtmp://12345.publishstream.cdnsun.net/P12345
- Username - P12345
- Password - Kmx7s14We
How these values map to FFmpeg
- Server URL is the RTMP destination shown on Services/How-To.
- Username and Password are required only for CDN Live service.
- Stream name is appended to the publishing URL, for example mystream.
- Service Domain is your playback domain, for example cdn.mycompany.com.
Publish a stream to the CDN using FFmpeg
When your source is already compatible
If your input is already encoded with H.264 video codec and AAC or MP3 audio codec, FFmpeg can pass the stream through without re-encoding. The -re option makes FFmpeg send the input at realtime speed, which is useful when streaming a file as if it were live.
CDN HTTP Live service
ffmpeg -re -i ./myvideo.mp4 -acodec copy -vcodec copy -f flv "rtmp://push-1.cdnsun.com/p12345/mystream"
CDN Live service
ffmpeg -re -i ./myvideo.mp4 -acodec copy -vcodec copy -f flv "rtmp://P12345:Kmx7s14We@12345.publishstream.cdnsun.net/P12345/mystream"
When your source must be re-encoded
If your input does not already match the required codecs, re-encode it to H.264 video and AAC audio before publishing.
CDN HTTP Live service
ffmpeg -re -i ./myvideo.mov -c:v libx264 -preset veryfast -pix_fmt yuv420p -c:a aac -b:a 128k -ar 44100 -f flv "rtmp://push-1.cdnsun.com/p12345/mystream"
CDN Live service
ffmpeg -re -i ./myvideo.mov -c:v libx264 -preset veryfast -pix_fmt yuv420p -c:a aac -b:a 128k -ar 44100 -f flv "rtmp://P12345:Kmx7s14We@12345.publishstream.cdnsun.net/P12345/mystream"
Please note that for RTMP Push and RTMP Pull workflows the published stream must use H.264 video codec and AAC or MP3 audio codec. The resulting RTMP output is published in FLV container format.
Accessing and verifying your CDN stream
The above commands publish your stream using mystream as the stream name. Assuming cdn.mycompany.com is the Service Domain of your CDN service, the stream will be accessible as follows.
CDN HTTP Live service
- HLS - https://cdn.mycompany.com/mystream/playlist.m3u8
CDN Live service
- RTMP - rtmp://cdn.mycompany.com/mystream
- HLS - https://cdn.mycompany.com/12345/_definst_/mystream/playlist.m3u8
Quick verification steps
- Confirm that FFmpeg keeps running and prints frame counters without repeated connection errors.
- Open the resulting HLS URL in your player or in the test player.
- You can also test HLS playback directly with FFmpeg tools, for example ffplay https://cdn.mycompany.com/mystream/playlist.m3u8.
Troubleshooting
Authentication support in FFmpeg for CDN Live service
CDN Live service requires FFmpeg build support for authentication because the publishing URL includes both username and password. The same type of authentication is used by OBS. Please note that it is not possible to disable authentication on CDN Live services.
How can I verify that my FFmpeg supports authentication?
Run the FFmpeg command with -loglevel debug just after ffmpeg. If you will see output similar to the following then your FFmpeg does not support authentication.
Parsing...
Parsed protocol: 0
Parsed host : P12345
Parsed app : P12345
RTMP_Connect0, failed to connect socket. 111 (Connection refused)
Please note that P12345 above corresponds to the username of your CDN Live service. If your FFmpeg supports authentication then you will see output similar to the following.
Parsing a group of options: output file rtmp://P12345:Kmx7s14We@12345.publishstream.cdnsun.net/P12345/mystream
Applying option acodec (force audio codec ('copy' to copy stream)) with argument copy.
Applying option vcodec (force video codec ('copy' to copy stream)) with argument copy.
Applying option f (force format) with argument flv.
Successfully parsed a group of options.
Opening an output file: rtmp://P12345:Kmx7s14We@12345.publishstream.cdnsun.net/P12345/mystream
How can I enable authentication support in FFmpeg?
You need to compile FFmpeg without librtmp.
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-libmp3lame --enable-libopencore-amrnb \
--enable-libopencore-amrwb --enable-libtheora --enable-libvorbis \
--enable-libvpx --enable-libx264 --enable-nonfree --enable-version3
make
Please note that --enable-librtmp is missing in the above configure options.
Other common checks
- Make sure that the stream name is appended after the Server URL, for example /mystream.
- Make sure that you use the publishing URL for FFmpeg output and the Service Domain only for playback.
- If the source is not already H.264 with AAC or MP3, use the re-encoding examples above.
What next?
Read about the following topics.