Secure HLS encoding using AES-128 encryption with FFmpeg

CK CK 3788 
Created at  
1,139 0 0

To encrypt a video stream using AES-128 encryption with FFmpeg, you can use the -hls_key_info_file option for HLS (HTTP Live Streaming) encryption. AES-128 encryption is commonly applied to HLS streams, but you can adapt it for other formats if needed.

Here’s a general step-by-step guide for AES-128 encryption with HLS:

  1. Generate a key file:
    First, generate a random 128-bit (16-byte) key that will be used for encryption. You can do this using OpenSSL or similar tools.

    Example command to generate a key:

    openssl rand 16 > keyfile.key
    
  2. Create a key info file:
    The key info file tells FFmpeg how to apply the encryption. It contains the URL where the key will be available, the file path to the key, and an IV (Initialization Vector), which can be a random 16-byte value or can be omitted to let FFmpeg handle it.

    Example key_info file format:

    http://example.com/keyfile.key
    /pub/your_file_path/keyfile.key
    0123456789abcdef0123456789abcdef
    
    • The first line is the URL where the key will be accessible.
    • The second line is the path to the key file on your local machine.
    • The third line is the IV (optional). If you omit this line, FFmpeg will automatically generate an IV for each segment.
  3. Use FFmpeg to encrypt and create the HLS playlist:
    Now you can use FFmpeg to apply AES-128 encryption to the video while creating HLS segments.

    Example command:

    ffmpeg -i input.mp4 -hls_time 10 -hls_key_info_file key_info -hls_playlist_type vod -hls_segment_filename 'segment_%03d.ts' output.m3u8
    
    • -i input.mp4: Specifies the input video.
    • -hls_time 10: Splits the video into segments of 10 seconds.
    • -hls_key_info_file key_info: Uses the key info file created earlier to apply AES-128 encryption.
    • -hls_playlist_type vod: Sets the playlist to "Video on Demand."
    • -hls_segment_filename 'segment_%03d.ts': Defines the pattern for naming the segments.
    • output.m3u8: Specifies the output HLS playlist file.

Notes:

  • Make sure the key file is hosted at a publicly accessible URL for the players to decrypt the video.
  • The generated HLS .m3u8 playlist file will contain references to the encrypted .ts segments, and the encryption key URL.

This process is commonly used for delivering encrypted HLS streams for secure video streaming applications.


Associated Data for Secure HLS Encoding using AES-128 Encryption with FFmpeg:

Secure HLS encoding using AES-128 encryption with FFmpeg
Key Concepts:

  • HLS (HTTP Live Streaming): A streaming protocol used for delivering video and audio content over HTTP. It is commonly used for online video platforms and live streaming applications.
  • AES-128 Encryption: A symmetric encryption algorithm that uses a 128-bit key to encrypt and decrypt data.
  • Key File: Contains the 128-bit key used for encryption.
  • Key Info File: Provides information on how to apply the encryption, including the key URL, file path, and optional IV.
  • IV (Initialization Vector): A random value used to ensure that the encryption of the same data with the same key produces different ciphertext.
  • FFmpeg: A powerful multimedia framework used for encoding, decoding, transcoding, and manipulating audio and video files.

Steps involved:

  • Generate a Key File: Generate a random 16-byte (128-bit) key using a tool like OpenSSL.
  • Create a Key Info File: Specify the key file path, key URL, and optional IV for FFmpeg to apply encryption.
  • Use FFmpeg to Encrypt and Create HLS Playlist: Run FFmpeg with the necessary options, including the key info file and playlist type, to create encrypted HLS segments and a playlist file.

If you just want to bypass the key value through parameter:

ffmpeg -i input.mp4 \
-hls_time 10 \
-hls_segment_type mpegts \
-hls_key_info_file <(echo "inlinekey 0123456789abcdef0123456789abcdef 0123456789abcdef0123456789abcdef") \
-hls_segment_filename 'segment_%03d.ts' \
-hls_playlist_type vod \
output.m3u8

If you want to use the key located on the remote server:

ffmpeg -i input.mp4 \
-hls_time 10 \
-hls_key_info_file <(echo "https://example.com/path/to/key
0123456789abcdef0123456789abcdef
0123456789abcdef0123456789abcdef") \
-hls_segment_filename 'segment_%03d.ts' \
-hls_playlist_type vod \
output.m3u8

M3U8 file example generated

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-KEY:METHOD=AES-128,URI="inlinekey",IV=0x0123456789abcdef0123456789abcdef
#EXTINF:10.000,
segment_000.ts
#EXTINF:10.000,
segment_001.ts
#EXTINF:10.000,
segment_002.ts
#EXTINF:10.000,
segment_003.ts
#EXTINF:10.000,
segment_004.ts
#EXTINF:10.000,
segment_005.ts
#EXTINF:10.000,
segment_006.ts
#EXTINF:10.000,
segment_007.ts
#EXT-X-ENDLIST

Benefits:

  • Secure Video Streaming: Prevents unauthorized access to video content.
  • Widely Supported: HLS and AES-128 encryption are industry standards supported by major players.

Additional Information:

  • Key Management: Securely managing and distributing the encryption key is crucial.
  • Content Delivery Network (CDN): Utilizing a CDN can help improve performance and reliability for delivering encrypted HLS streams.
  • Player Compatibility: Ensure that the video players used to access the HLS streams are compatible with AES-128 encryption.

 

Can we save the key file remotely instead of inline?

If you want to set the key more securly, you can locate the key on the server, and set the m3u8 like below:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-KEY:METHOD=AES-128,URI="https://example.com/path/to/keyfile",IV=0x0123456789abcdef0123456789abcdef
#EXTINF:10.000,
segment_000.ts
#EXTINF:10.000,
segment_001.ts
#EXTINF:10.000,
segment_002.ts
#EXTINF:10.000,
segment_003.ts
#EXTINF:10.000,
segment_004.ts
#EXTINF:10.000,
segment_005.ts
#EXTINF:10.000,
segment_006.ts
#EXTINF:10.000,
segment_007.ts
#EXT-X-ENDLIST
Tags AES-128 Encryption FFmpeg HLS (HTTP Live Streaming) Video Encryption Video Streaming Security Facebook X
Comments 0
Similar posts
  1. How do I create animated background for Google Meet?
    21,041
  2. FFmpeg CLI to remove audio track from MP4
    543
  3. FFmpeg compile option to support SCTE-35
    840
  4. Script checking internet restriction with FFmpeg
    684
  5. FFmpeg compile options based on configure
    1,112
  6. FFmpeg CLI to mix two different audios in a single audio file such as mp3
    604
  7. FFmpeg CLI to merge a video only mp4 and mp3 audio file
    335
  8. FFmpeg CLI to convert a video file to an MP3 audio file
    349
  1. From SDI to MXL: Inside the Cloud-Native Infrastructure Powering Next-Gen Live Broadcast
    570
  2. Smooth switching with EXT-X-DISCONTINUITY in HLS
    433
  3. Disabling Audio Tracks in HLS.js
    376
  4. A Deep Dive Into Internet Advertising Ecosystem on Video Streaming Industry
    647
  5. When to use #EXT-X-DISCONTINUITY in HLS?
    1,428
  6. A prefect Apache server settings to avoid CORS error for HLS
    605
  7. CloudFront Settings for Dynamic HLS Streaming
    651
  8. SafeFrame is a web standard developed by IAB
    510
  9. CSAI vs SSAI
    578
  10. Ad Marker: SCTE-35 Message Encoder based in Python
    800
  11. Ad marker: SCTE-35 enhanced
    575
  12. HTTP Proxy when your URL access is blocked with FFmpeg
    1,902
  13. Limit Maximum Resolution in HLS.js
    665
  14. Magic line when HLS video manifest is mixed with encrypted / unencrypted video chunks
    1,464
  15. Recovering a crashed HLS.js player
    1,282
  16. Machine Learning Models for Ad Detection from a Video Stream
    189
  17. Unlock Flawless HLS Audio Sync: The Power of #EXT-X-INDEPENDENT-SEGMENTS
    377
  18. NVIDIA CUDA library which can be integrated with C/C++
    376
  19. Approaches to Scene Detection in Video Processing
    646
  20. Building GPU as a Service aka Dockerized GPU
    1,580
  21. IAB Categories (a.k.a. OpenRTB Code)
    1,014
  22. FFmpeg library that can be directly integrated into C/C++/Rust/Python without CLI
    2,073
  23. HTTP Live Streaming (HLS) was developed by Apple as part of QuickTime X in iOS3.0, and became an official standard (RFC 8126) in 2017
    596
  24. Progressive MP4 is an application of streaming techniques using the standardized MP4 container invented by MPEG LA consortium
    1,078
  25. HLS.js options to support subtitle
    516
  26. When HLS.js need to load m3u8 URL redirecting to the different URL (302 Redirection)
    664
  27. HLS.js code to reload when user face bufferfull error
    705
  28. HLS.js code to reload video when levelLoadTimeOut occurs
    707
  29. Change the manifest loading timeout in HLS.js
    1,039
  30. HLS.js Custom Error Handler to load Main Manifest from unstable HLS Live Stream
    1,433
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    252
  4. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  5. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  6. Array of arguments passed to script in PHP
    90
  7. Invalid Auction Storms in Ad Tech
    98
  8. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  9. The Two Clocks of Ad Server Architecture
    70
  10. Frequency Cap Architecture Overview
    515
  11. Collecting Proof-of-Play from 100,000 Screens
    146
  12. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  13. What is RedisVL?
    109
  14. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  15. TPU & GPU Cluster Management Tools
    245
  16. The Concept of Scaffolding in AI Agents
    234
  17. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  18. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  19. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  20. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  21. Machine Learning: Training vs. Ranking
    222
  22. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  23. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  24. Designing a Data Curation System Around a Vector Database
    221
  25. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  26. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  27. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  28. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  29. Understanding Data Lineage
    263
  30. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    167