且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

FFmpeg:如何制作MP4 CENC(通用加密)视频

更新时间:2023-01-25 17:34:04

Mulvya的答案涵盖了ffmpeg-options.

Mulvya's answer covered the ffmpeg-options.

就像我昨天(独立地)做一些实验一样,我只是添加一个具体的示例并谈论回放.

I'm just adding a concrete example and talk about playback too, as i did some experiments yesterday (independently).

ffmpeg -i SampleVideo_1280x720_1mb.mp4 -vcodec copy -acodec copy -encryption_scheme cenc-aes-ctr -encryption_key 76a6c65c5ea762046bd749a2e632ccbb -encryption_kid a7e61c373e219033c21091fa607bf3b8 SampleVideo_1280x720_1mb_encrypted.mp4

(当然,根据您的情况,用法可能有所不同;我只是重新混合了视频和音频)

(of course usage might be different for your case; i just remuxed video and audio)

ffplay

ffplay SampleVideo_1280x720_1mb_encrypted.mp4 -decryption_key 76a6c65c5ea762046bd749a2e632ccbb

但是由于这或多或少是一个原型播放器,所以人们可能想使用更强大的功能.

But as this is more or less a prototype-player, one might want to use something more powerful.

mpv

mpv --demuxer-lavf-o=decryption_key=76a6c65c5ea762046bd749a2e632ccbb SampleVideo_1280x720_1mb_encrypted.mp4

此处进行了一些讨论,因为我的第一个预期命令行没有表现出预期!

There is some discussion here as my first expected command-line did not behave as expected!

编辑:尝试解决Reino的问题

trying to address Reino's questions

encryption_key仅为128位= 16字节编码为十六进制(遵循 random.org 的配置为16个字节,并且hex.encoding是有效的密钥(但我通常不建议您信任外部资源).我使用python的秘密模块归结为:secrets.token_hex(16) . encryption_key将需要用于解码.

The encryption_key is just 128 bit = 16 bytes encoded as Hex (following the usage of AES-128-CTR). So random.org with a configuration of 16 bytes and hex.encoding would be a valid key (but i'm not recommending to trust external resources in general). I used python's secrets module which boils down to: secrets.token_hex(16). This encryption_key will be needed for decoding.

encryption_kid 密钥ID 只是该键的标识符,可能是更复杂的使用模式所需要的(我!是在猜测!您可以执行以下操作:嘿视频...我需要我的1000个键中的哪一个? ).我想必须通过它,但是解码不是必需的(如果您知道对哪个视频使用哪个密钥).

The encryption_kid Key ID is just an identifier for this key, probably needed for more complex usage-patterns (i'm !guessing! you could do something like: hey video... which of my 1000 keys do i need for you?). I suppose it's mandatory to pass it, but it's not required for decoding (if you know which key to use for which video).

官方参考为:

  • 标准
  • ffmpeg实现:文档(可通过命令行获取)或简短的
  • Standard
  • ffmpeg implementation: docs (available through command-line) or a short extraction