FFmpeg 加密 mpegts 文件的处理方式

12380阅读 0评论2017-11-28 T-Bagwell
分类:LINUX

在FFmpeg中加密有很多种处理方式,FFmpeg中常见的加密是使用crypto来进行的,下面看一下crypto的文档描述部分:

点击(此处)折叠或打开

  1. AES-encrypted stream reading protocol.
  2. The accepted options are:
  3. key
  4. Set the AES decryption key binary block from given hexadecimal representation.
  5. iv
  6. Set the AES decryption initialization vector binary block from given hexadecimal representation.
  7. Accepted URL formats:
  8. crypto:URL
  9. crypto+URL

从内容中可以看到,crypto加密采用的是AES的加密,并且crypto在FFmpeg中是一种协议:可以使用ffmpeg -protocols查看协议列表:

点击(此处)折叠或打开

  1. liuqideMacBook-Pro:hls liuqi$ ffmpeg -protocols
  2. ffmpeg version N-89165-ga5870cb37f Copyright (c) 2000-2017 the FFmpeg developers
  3.   built with Apple LLVM version 9.0.0 (clang-900.0.38)
  4.   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
  5.   libavutil 56. 0.100 / 56. 0.100
  6.   libavcodec 58. 3.105 / 58. 3.105
  7.   libavformat 58. 2.102 / 58. 2.102
  8.   libavdevice 58. 0.100 / 58. 0.100
  9.   libavfilter 7. 2.100 / 7. 2.100
  10.   libswscale 5. 0.101 / 5. 0.101
  11.   libswresample 3. 0.101 / 3. 0.101
  12.   libpostproc 55. 0.100 / 55. 0.100
  13. Supported file protocols:
  14. Input:
  15.   async
  16.   bluray
  17.   cache
  18.   concat
  19.   crypto
  20.   data
  21.   ffrtmphttp
  22.   file
  23.   ftp
  24.   gopher
  25.   hls
  26.   http
  27.   httpproxy
  28.   https
  29.   mmsh
  30.   mmst
  31.   pipe
  32.   rtmp
  33.   rtmps
  34.   rtmpt
  35.   rtmpts
  36.   rtp
  37.   srtp
  38.   subfile
  39.   tcp
  40.   tls
  41.   udp
  42.   udplite
  43.   unix
  44. Output:
  45.   crypto
  46.   ffrtmphttp
  47.   file
  48.   ftp
  49.   gopher
  50.   http
  51.   httpproxy
  52.   https
  53.   icecast
  54.   md5
  55.   pipe
  56.   prompeg
  57.   rtmp
  58.   rtmps
  59.   rtmpt
  60.   rtmpts
  61.   rtp
  62.   srtp
  63.   tee
  64.   tcp
  65.   tls
  66.   udp
  67.   udplite
  68.   unix
  69. liuqideMacBook-Pro:hls liuqi$
根据命令行执行后输出的信息可以看到,输入与输出协议中均包含crypto协议,从文档中的描述中可以清楚地了解到有crypto有两个参数,分别是:key与iv。而crypto的使用形式有两种:crypto:URL 与 crypto+URL 两种格式,下面就一个例子:
首先自己生成一个key串:

点击(此处)折叠或打开

  1. liuqideMacBook-Pro:ffmpeg liuqi$ echo $(openssl rand -hex 16)
  2. 855582bceff7de4c3590bf1640c94f05
2. 生成一个iv串:

点击(此处)折叠或打开

  1. 0123456789ABCDEF0123456789ABCDEF
3. 执行ffmpeg命令生成加密的mpegts:
输出信息:

点击(此处)折叠或打开

  1. liuqideMacBook-Pro:hls liuqi$ ffmpeg -i ~/Movies/objectC/bbs.chinaffmpeg.com孙悟空.mp4 -c copy -f mpegts -key "855582bceff7de4c3590bf1640c94f05" -iv "0123456789ABCDEF0123456789ABCDEF" crypto:output.mpegts
  2. ffmpeg version bbs.chinaffmpeg.com孙悟空专属版本 Copyright (c) 2000-2017 the FFmpeg developers
  3.   built with Apple LLVM version 9.0.0 (clang-900.0.38)
  4.   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
  5.   libavutil 56. 0.100 / 56. 0.100
  6.   libavcodec 58. 3.105 / 58. 3.105
  7.   libavformat 58. 2.102 / 58. 2.102
  8.   libavdevice 58. 0.100 / 58. 0.100
  9.   libavfilter 7. 2.100 / 7. 2.100
  10.   libswscale 5. 0.101 / 5. 0.101
  11.   libswresample 3. 0.101 / 3. 0.101
  12.   libpostproc 55. 0.100 / 55. 0.100
  13. Input #0, mov,mp4,m4a,3gp,3g2,mj2, from ‘/Users/liuqi/Movies/objectC/bbs.chinaffmpeg.com孙悟空.mp4’:
  14.   Metadata:
  15.     major_brand : mp42
  16.     minor_version : 0
  17.     compatible_brands: isommp42
  18.     creation_time : 2016-08-13T00:36:19.000000Z
  19.   Duration: 00:03:18.79, start: 0.000000, bitrate: 1849 kb/s
  20.     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)
  21.     Metadata:
  22.       creation_time : 2016-08-13T00:36:19.000000Z
  23.       handler_name : ISO Media file produced by Google Inc.
  24.     Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
  25.     Metadata:
  26.       creation_time : 2016-08-13T00:36:19.000000Z
  27.       handler_name : ISO Media file produced by Google Inc.
  28. Output #0, mpegts, to 'crypto:output.mpegts':
  29.   Metadata:
  30.     major_brand : mp42
  31.     minor_version : 0
  32.     compatible_brands: isommp42
  33.     encoder : Lavf58.2.102
  34.     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)
  35.     Metadata:
  36.       creation_time : 2016-08-13T00:36:19.000000Z
  37.       handler_name : ISO Media file produced by Google Inc.
  38.     Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
  39.     Metadata:
  40.       creation_time : 2016-08-13T00:36:19.000000Z
  41.       handler_name : ISO Media file produced by Google Inc.
  42. Stream mapping:
  43.   Stream #0:0 -> #0:0 (copy)
  44.   Stream #0:1 -> #0:1 (copy)
  45. Press [q] to stop, [?] for help
  46. frame= 5957 fps=0.0 q=-1.0 Lsize= 49176kB time=00:03:18.76 bitrate=2026.8kbits/s speed= 224x
  47. video:41725kB audio:3048kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 9.836025%
生成的mpegts文件只有用key与iv才能够播放,否则会播放错误:

1. 使用key与iv正常播放:

点击(此处)折叠或打开

  1. liuqideMacBook-Pro:hls liuqi$ ffmpeg -key "855582bceff7de4c3590bf1640c94f05" -iv "0123456789ABCDEF0123456789ABCDEA" -i crypto:output.mpegts
  2. Input #0, mpegts, from 'crypto:output.mpegts':
  3.   Duration: 00:03:18.77, start: 1.433367, bitrate: 2026 kb/s
  4.   Program 1
  5.     Metadata:
  6.       service_name : Service01
  7.       service_provider: FFmpeg
  8.     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
  9.     Stream #0:1[0x101](und): Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 126 kb/s
  10. At least one output file must be specified
2. 使用错误的key与iv播放:

点击(此处)折叠或打开

  1. liuqideMacBook-Pro:hls liuqi$ ffmpeg -key "355582bceff7de4c3590bf1640c94f05" -iv "0123456789ABCDEF0123456789ABCDEA" -i crypto:output.mpegts
  2. crypto:output.mpegts: Invalid data found when processing input
3. 不用key播放:

点击(此处)折叠或打开

  1. liuqideMacBook-Pro:hls liuqi$ ffmpeg -i output.mpegts
  2. output.mpegts: Invalid data found when processing input
4. 确认文件是否存在:

点击(此处)折叠或打开

  1. liuqideMacBook-Pro:hls liuqi$ ls output.mpegts
  2. output.mpegts
  3. liuqideMacBook-Pro:hls liuqi$
到这里,使用ffmpeg为mpegts加密相关介绍完毕。

上一篇:从视频中每X秒创建一个缩略图
下一篇:使用FFmpeg新解码API解封装解码音视频(代码实例)