目錄
- 參考
- 概述
- CENC加密的mp4文件
- libav CENC加密礼患、解密mp4文件
1. 參考
- [1] ISO/IEC 23001-7:2016
Information technology -- MPEG systems technologies -- Part 7: Common encryption in ISO base media file format files - [2] ISO Common Encryption ('cenc') Protection Scheme for ISO Base Media File Format Stream Format
- [3] stackoverflow/FFmpeg: how to produce MP4 CENC (Common Encryption) videos
- [4] 郭曉霞/視頻內(nèi)容加密封裝技術研究
- [5] zhihu/代碼實現(xiàn)FFmpeg加密叔锐、解密mp4文件
2. 概述
FFmpeg提供了cenc的方式來加密保護mp4文件。
ffmpeg -h muxer=mp4
能看到mp4封裝器提供的選項马篮,加密相關的選項如下所示晤柄。
-encryption_scheme <string> E....... Configures the encryption scheme, allowed values are none, cenc-aes-ctr
-encryption_key <binary> E....... The media encryption key (hex)
-encryption_kid <binary> E....... The media encryption key identifier (hex)
- encryption_scheme:加密的方案粟判,只支持cenc-aes-ctr亿昏。
- encryption_key:加密key,128比特(16bytes)档礁,格式使用16進制的表示角钩。加密和解密的時候使用。
- encryption_kid:加密key的標識,128比特(16bytes)彤断,格式使用16進制的表示野舶。加密時需要,解密不需要宰衙。具體作用尚未找到參考平道。
ffmpeg命令行工具對已有mp4進行CENC加密示例:
ffmpeg -i julin_5s.mp4 -c:v copy -c:a copy -encryption_scheme cenc-aes-ctr -encryption_key 76a6c65c5ea762046bd749a2e632ccbb -encryption_kid a7e61c373e219033c21091fa607bf3b8 julin_5s_cenc.mp4
ffplay播放CENC加密的mp4文件的示例:
ffplay julin_5s_cenc.mp4 -decryption_key 76a6c65c5ea762046bd749a2e632ccbb
3. mp4的CENC加密文件
image.png
查看CENC加密后的mp4文件,相比原有文件在stbl下增加了3個box:
- senc:sample specific encryption data供炼,特定加密數(shù)據(jù)樣本一屋。
- saio:sample auxiliary information offsets,樣本輔助信息偏移量袋哼。
- saiz:sample auxiliary information sizes冀墨,樣本輔助信息大小。
4. libav CENC加密涛贯、解密mp4文件
加密
AVDictionary *opts = NULL;
av_dict_set(&format_opts, "encryption_scheme", "cenc-aes-ctr", 0);
av_dict_set(&format_opts, "encryption_key", "76a6c65c5ea762046bd749a2e632ccbb", 0);
av_dict_set(&format_opts, "encryption_kid", "a7e61c373e219033c21091fa607bf3b8", 0);
ret = avformat_write_header(ofmt_ctx, &format_opts);
解密
av_dict_set(&format_opts, "decryption_key", "76a6c65c5ea762046bd749a2e632ccbb", 0);
ret = avformat_open_input(&ifmt_ctx, is->filename, is->iformat, &format_opts);