1.环境
[root@node2 ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.8 (Santiago) [root@node2 ~]# uname -a Linux node2 2.6.32-642.el6.x86_64 #1 SMP Wed Apr 13 00:51:26 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux [root@node2 ~]#
2.ffpmeg安装
1.https://ffmpeg.org/download.html#build-linux
2.
3.下载用wget或者迅雷
wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz
4.如果想要ffmpeg命令全局可用,可以在/usr/bin目录加个链接
cd /usr/bin ln -s /root/ffmpeg-git-20191105-amd64-static/ffmpeg ffmpeg
3.基本使用
1.命令
ffmpeg -y -i test.mp4 -hls_time 50 -hls_playlist_type vod -hls_segment_filename "video/file%d.ts" playlist.m3u8
-i test.mp4 输入文件test.mp4
-hls_time 50 分隔的每小段是50s
-hls_playlist_type vod 播放类型 vod 是点播,表示PlayList不会变
-hls_segment_filename "video/file%d.ts" 首先建立video文件夹,文件名都是类似file0.ts
playlist.m3u8 m3u8的文件名
4.加密
key和iv是对称加密生成加密串要求的两个变量。
key就是自定义加密key,自己定义的简单串;
iv是initialization vector的意思,就是加密的初始话矢量,初始化加密函数的变量。
加密用的 key
xxd enc.key 查看生成的enc.key
0000000: 6883 86c2 b465 eac5 b5a4 7a22 ffe1 1d00 h....e....z"....
openssl rand 16 > enc.key (生成一个enc.key文件)
生成 iv(偏移量)
openssl rand -hex 16 (生成一段字符串,记下来)
64f362fe759fcd5f36570ef03fab696f
新建一个文件 enc.keyinfo
[root@node2 ~]# cat enc.keyinfo http://localhost/video/enc.key enc.key 64f362fe759fcd5f36570ef03fab696f
http://localhost/video/enc.key enc.key的路径,使用http形式 Key URI key=“File@E://enc.key”
enc.key Path to key file
64f362fe759fcd5f36570ef03fab696f iv
命令:
ffmpeg -y -i test.mp4 -hls_time 50 -hls_playlist_type vod -hls_segment_filename "video/file%d.ts" -hls_key_info_file enc.keyinfo playlist.m3u8
-hls_key_info_file enc.keyinfo
5.解密使用
[root@node2 ~]# hexdump -v -e '16/1 "%02x"' enc.key deb7bc285a864bbd9073d72250829728
开发时经常会需查看非文本文件内容,最常见的16进制查看器就是hexdump
-v 不要省略
-e 指定格式字符串,格式字符串由单引号包含,格式字符串形如:’a/b “format1” “format2”
%02x:两位十六进制
命令
openssl aes-128-cbc -d -in env.ts -out media_decryptd_0.ts -nosalt -iv 00000000000000000000000000000000 -K deb7bc285a864bbd9073d72250829728
-K deb7bc285a864bbd9073d72250829728 密钥16进制查看器就是hexdump 看的enc.key
-iv printf '%032x' $index index 就是file%d.ts 中的d
报错:
[root@node2 sucai]# openssl aes-128-cbc -d -in e_0.ts -out media_decryptd_1.ts -nosalt -iv 0fe82567a6be41afda68d82d3724976a -K 6749306b703646774d4a334c316a6 645bad decrypt 140324060514048:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:crypto/evp/evp_enc.c:536:
I found this, which suggests a bad password:
http://www.eecis.udel.edu/wiki/ececis-docs/index.php/FAQ/Applications#toc22
To decrypt (notice the -d for decryption) the file created in the
previous example do the following:
% openssl enc -d -in ciphertextout -out outputfile -aes256
enter aes-256-cbc decryption password:
If the password is correct the plaintext will appear in outputfile. Be
sure to delete or protect this file when done. At all times also make
sure that standard permissions would not allow someone to read the
plaintext file.
If an incorrect password is enter something like this will be displayed:
bad decrypt
11044:error:06065064:digital envelope routines:EVP_DecryptFinal:bad
decrypt:evp_enc.c:450:
参考资料:
1.https://blog.csdn.net/nizhengjia888/article/details/78041945
2.https://blog.csdn.net/PM_605/article/details/80076850
3.https://github.com/huwan/FFmpeg-Tutorial-CN