Stream Video Over HTTPS with FFmpeg
This article provides a quick guide on how to securely stream video over HTTP (HTTPS) using FFmpeg. You will learn how to configure FFmpeg to package your video into the HTTP Live Streaming (HLS) format and securely upload or stream it to an HTTPS-enabled web server or ingestion endpoint.
To stream video over HTTPS using FFmpeg, the most common and reliable method is using the HTTP Live Streaming (HLS) protocol. FFmpeg encodes the video, cuts it into small segments, creates a playlist file (.m3u8), and sends these files to a secure web server (like Nginx or Apache configured with SSL/TLS) via HTTPS.
Step 1: Prepare Your HTTPS Destination
Before running FFmpeg, you need a web server or a streaming destination configured to accept HTTPS uploads (usually via HTTP PUT or POST methods, such as WebDAV). Ensure your server has a valid SSL certificate installed.
Step 2: Use the FFmpeg HLS Command
To encode a video input and push it to your secure server over HTTPS, use the following FFmpeg command:
ffmpeg -re -i input.mp4 \
-c:v libx264 -preset veryfast -b:v 3000k \
-c:a aac -b:a 128k \
-f hls \
-hls_time 4 \
-hls_list_size 5 \
-hls_flags delete_segments \
-method PUT \
https://yoursecure-server.com/live/stream.m3u8Command Breakdown:
-re: Tells FFmpeg to read the input in real-time (essential for live streaming).-i input.mp4: Your source video (can be a local file, webcam, or RTMP feed).-c:v libx264 -c:a aac: Encodes the video to H.264 and audio to AAC, which are highly compatible with web browsers.-f hls: Specifies the output format as HLS.-hls_time 4: Sets the segment duration to 4 seconds.-hls_list_size 5: Keeps only the 5 most recent segments in the playlist (ideal for live streams to save disk space).-hls_flags delete_segments: Automatically deletes old segments as they fall off the playlist.-method PUT: Tells FFmpeg to upload the.tssegments and.m3u8playlist to the remote server using the HTTP PUT method.https://...: The secure HTTPS destination URL.
Step 3: Handling SSL Certificate Issues
If you are using a self-signed SSL certificate for testing purposes,
FFmpeg might reject the connection due to verification errors. You can
bypass certificate verification by adding the -tls_verify 0
flag to your command:
ffmpeg -tls_verify 0 -re -i input.mp4 -c:v libx264 -c:a aac -f hls -method PUT https://yoursecure-server.com/live/stream.m3u8Note: Only use -tls_verify 0 for development and
testing. Always use valid certificates in production.
Step 4: Playing Back the Secure Stream
Once FFmpeg starts uploading the segments to your HTTPS server, users can watch the stream securely. Any modern HLS-compatible web player (such as Video.js, hls.js, or Plyr) can play the stream by pointing to the secure URL:
https://yoursecure-server.com/live/stream.m3u8