README
¶
stream-encrypt
HTTP streaming server that encrypts and refragments MP4 files on-the-fly using the mp4.StreamFile API.
Features
- HTTP Streaming: Serves MP4 files via HTTP with chunked transfer encoding
- Refragmentation: Splits input fragments into smaller output fragments with configurable sample count
- Encryption: Encrypts fragments using Common Encryption (CENC or CBCS)
- Low Latency: Uses
GetSampleRange()for minimal buffering and immediate delivery - Sequence Number Preservation: Sub-fragments maintain the same sequence number as their parent
Usage
Basic Streaming (No Encryption, No Refragmentation)
go run *.go
curl http://localhost:8080/enc.mp4 -o output.mp4
Using a Custom Input File
go run *.go -input /path/to/your/video.mp4
curl http://localhost:8080/enc.mp4 -o output.mp4
Refragmentation
Split fragments to 30 samples each:
go run *.go -samples 30
curl http://localhost:8080/enc.mp4 -o refragmented.mp4
Encryption with Refragmentation
go run *.go \
-samples 30 \
-key 11223344556677889900aabbccddeeff \
-keyid 00112233445566778899aabbccddeeff \
-iv 00000000000000000000000000000000 \
-scheme cenc
curl http://localhost:8080/enc.mp4 -o encrypted.mp4
Command-Line Options
-input string
Input MP4 file path (default "../../mp4/testdata/v300_multiple_segments.mp4")
-port int
HTTP server port (default 8080)
-samples int
Samples per fragment (0=no refrag) (default 0)
-key string
Encryption key (hex)
-keyid string
Key ID (hex)
-iv string
IV (hex)
-scheme string
Encryption scheme (cenc/cbcs) (default "cenc")
How It Works
Streaming Pipeline
Input File → StreamFile.InitDecodeStream() →
[Refragment?] → [Encrypt?] → HTTP Response (Chunked) → Client
- Init Segment: Read and optionally modify for encryption, write immediately
- Fragment Processing: For each input fragment:
- Use
GetSampleRange()to fetch only needed samples - Create sub-fragments if refragmentation enabled
- Encrypt if encryption configured
- Write and flush immediately
- Use
Refragmentation Strategy
- Input: Fragment with 60 samples
- Output (samplesPerFrag=30): Two fragments with 30 samples each
- Sequence Numbers: Both sub-fragments keep the parent's sequence number
- Benefits: Lower latency, smaller chunk sizes for adaptive streaming
Encryption
- Schemes: CENC (AES-CTR) or CBCS (AES-CBC)
- IV Derivation: Incremental IV per fragment based on fragment number
- Metadata: Adds
senc,saiz,saioboxes to fragments - Init Modification: Converts sample entries to
encv/enca, addssinfstructure
Testing
Run all tests:
go test -v
Individual test steps:
go test -v -run TestStep1 # Basic streaming
go test -v -run TestStep2 # Refragmentation
go test -v -run TestStep3 # Encryption
Implementation Files
- main.go: HTTP server and request handling
- refragment.go: Fragment splitting using
GetSampleRange() - encryptor.go: Encryption setup and per-fragment encryption
- main_test.go: Integration tests for all features
Design Principles
- Streaming-First: Never buffer entire fragments, use
GetSampleRange()instead ofGetSamples() - Memory Efficient: Process and deliver each sub-fragment immediately
- Sequence Number Preservation: All sub-fragments from same input share sequence number
- Reuse Existing Code: Leverages
mp4.InitProtect()andmp4.EncryptFragment()
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.