在FFmpeg中加密有很多种处理方式,FFmpeg中常见的加密是使用crypto来进行的,下面看一下crypto的文档描述部分:
AES-encrypted stream reading protocol.
The accepted options are:
key
Set the AES decryption key binary block from given hexadecimal representation.
iv
Set the AES decryption initialization vector binary block from given hexadecimal representation.
Accepted URL formats:
crypto:URL
crypto+URL
从内容中可以看到,crypto加密采用的是AES的加密,并且crypto在FFmpeg中是一种协议:可以使用ffmpeg -protocols查看协议列表:
liuqideMacBook-Pro:hls liuqi$ ffmpeg -protocols
ffmpeg version N-89165-ga5870cb37f Copyright (c) 2000-2017 the FFmpeg developers
built with Apple LLVM version 9.0.0 (clang-900.0.38)
configuration: --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libspeex --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-version3 --cc='ccache gcc' --enable-nonfree --enable-videotoolbox
libavutil 56. 0.100 / 56. 0.100
libavcodec 58. 3.105 / 58. 3.105
libavformat 58. 2.102 / 58. 2.102
libavdevice 58. 0.100 / 58. 0.100
libavfilter 7. 2.100 / 7. 2.100
libswscale 5. 0.101 / 5. 0.101
libswresample 3. 0.101 / 3. 0.101
libpostproc 55. 0.100 / 55. 0.100
Supported file protocols:
Input:
async
bluray
cache
concat
crypto
data
ffrtmphttp
file
ftp
gopher
hls
http
httpproxy
https
mmsh
mmst
pipe
rtmp
rtmps
rtmpt
rtmpts
rtp
srtp
subfile
tcp
tls
udp
udplite
unix
Output:
crypto
ffrtmphttp
file
ftp
gopher
http
httpproxy
https
icecast
md5
pipe
prompeg
rtmp
rtmps
rtmpt
rtmpts
rtp
srtp
tee
tcp
tls
udp
udplite
unix
liuqideMacBook-Pro:hls liuqi$
根据命令行执行后输出的信息可以看到,输入与输出协议中均包含crypto协议,从文档中的描述中可以清楚地了解到有crypto有两个参数,分别是:key与iv。而crypto的使用形式有两种:crypto:URL 与 crypto+URL 两种格式,下面就一个例子:
1、首先自己生成一个key串
liuqideMacBook-Pro:ffmpeg liuqi$ echo $(openssl rand -hex 16)
855582bceff7de4c3590bf1640c94f05
2、 生成一个iv串
0123456789ABCDEF0123456789ABCDEF
3、执行ffmpeg命令生成加密的mpegts
输出信息:
liuqideMacBook-Pro:hls liuqi$ ffmpeg -i ~/Movies/objectC/bbs.chinaffmpeg.com孙悟空.mp4 -c copy -f mpegts -key "855582bceff7de4c3590bf1640c94f05" -iv "0123456789ABCDEF0123456789ABCDEF" crypto:output.mpegts
ffmpeg version bbs.chinaffmpeg.com孙悟空专属版本 Copyright (c) 2000-2017 the FFmpeg developers
built with Apple LLVM version 9.0.0 (clang-900.0.38)
configuration: --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libspeex --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-version3 --cc='ccache gcc' --enable-nonfree --enable-videotoolbox
libavutil 56. 0.100 / 56. 0.100
libavcodec 58. 3.105 / 58. 3.105
libavformat 58. 2.102 / 58. 2.102
libavdevice 58. 0.100 / 58. 0.100
libavfilter 7. 2.100 / 7. 2.100
libswscale 5. 0.101 / 5. 0.101
libswresample 3. 0.101 / 3. 0.101
libpostproc 55. 0.100 / 55. 0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from ‘/Users/liuqi/Movies/objectC/bbs.chinaffmpeg.com孙悟空.mp4’:
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
creation_time : 2016-08-13T00:36:19.000000Z
Duration: 00:03:18.79, start: 0.000000, bitrate: 1849 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 1719 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc (default)
Metadata:
creation_time : 2016-08-13T00:36:19.000000Z
handler_name : ISO Media file produced by Google Inc.
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
Metadata:
creation_time : 2016-08-13T00:36:19.000000Z
handler_name : ISO Media file produced by Google Inc.
Output #0, mpegts, to 'crypto:output.mpegts':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
encoder : Lavf58.2.102
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 1719 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 90k tbc (default)
Metadata:
creation_time : 2016-08-13T00:36:19.000000Z
handler_name : ISO Media file produced by Google Inc.
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
Metadata:
creation_time : 2016-08-13T00:36:19.000000Z
handler_name : ISO Media file produced by Google Inc.
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 5957 fps=0.0 q=-1.0 Lsize= 49176kB time=00:03:18.76 bitrate=2026.8kbits/s speed= 224x
video:41725kB audio:3048kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 9.836025%
生成的mpegts文件只有用key与iv才能够播放,否则会播放错误:
1、使用key与iv正常播放
liuqideMacBook-Pro:hls liuqi$ ffmpeg -key "855582bceff7de4c3590bf1640c94f05" -iv "0123456789ABCDEF0123456789ABCDEA" -i crypto:output.mpegts
Input #0, mpegts, from 'crypto:output.mpegts':
Duration: 00:03:18.77, start: 1.433367, bitrate: 2026 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:1[0x101](und): Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 126 kb/s
At least one output file must be specified
2、使用错误的key与iv播放
liuqideMacBook-Pro:hls liuqi$ ffmpeg -key "355582bceff7de4c3590bf1640c94f05" -iv "0123456789ABCDEF0123456789ABCDEA" -i crypto:output.mpegts
crypto:output.mpegts: Invalid data found when processing input
3、不用key播放
liuqideMacBook-Pro:hls liuqi$ ffmpeg -i output.mpegts
output.mpegts: Invalid data found when processing input
4、确认文件是否存在
liuqideMacBook-Pro:hls liuqi$ ls output.mpegts
output.mpegts
liuqideMacBook-Pro:hls liuqi$
到这里,使用ffmpeg为mpegts加密相关介绍完毕。