加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

ffmpeg切割TS脚本带加密

发布时间:2020-12-14 22:54:58 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/bin/bash# Be warned!!! there is an "rm -r" at the end of this script. I do not guarantee anything,in particular that (mis)use won't damag

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/bin/bash

# Be warned!!! there is an "rm -r" at the end of this script.  I do not guarantee anything,in particular that (mis)use won't damage something important.
# Use at your own risk!
# Copyright public domain
# Derived from: http://dryize.wordpress.com/2014/04/02/protected-hls-using-ffmpeg-and-openssl/

set -e     # Exit on errors

keyFile='video.key'
vidFile="$1"
playList='cc.m3u8'    # Must end with .m3u8!
splitFilePrefix='stream'
wKeyPath="$3/$keyFile"

# Only run if the video file is readable and is not an irregular file
if [ -f "$vidFile" -a -r "$vidFile" ]; then
# Whole rest of program runs in this if; exits 2 at the end if the above fails.

# If folder exists,add it to our paths
if [ -d "$2" ]; then
outDir="$2"
else    # Try to make the directory
mkdir "$2" || exit 1
outDir="$2"
fi

tempDir="$outDir/.$$_tmp"
keyFile="$outDir/$keyFile"

mkdir $tempDir    # It is deleted later

# Stuff from the original script

openssl rand 16 > $keyFile
# I'm not sure if I have the quoting right on this. If not,try removing the double quotes in the middle.
encryptionKey=`cat $keyFile | hexdump -e '16/1 "%02x"'`

# Here's where I run ffmpeg
ffmpeg -i "$vidFile" -c copy -map 0 -bsf:v  h264_mp4toannexb  -flags 
global_header -f segment -segment_time 10 -segment_list_size 0 
-segment_list "$tempDir/$playList" -segment_format mpegts "$tempDir/$splitFilePrefix"%d.ts
# N.B. I question some of those parameters,such as segment_time,but this is not my command.
# I also had to use libfdk_aac instead of libvo_aacenc
# I thought m3u8 playlists needed full URLs,but he doesn't include them.

numberOfTsFiles=`ls "$tempDir/$splitFilePrefix"*.ts | wc -l`

for (( i=0; i<$numberOfTsFiles; i++ ))
do
initializationVector=`printf '%032x' $i`
openssl aes-128-cbc -e -in "$tempDir/$splitFilePrefix"$i.ts 
-out "$outDir/$splitFilePrefix"$i.ts -nosalt -iv $initializationVector -K $encryptionKey
done

# Write something to the middle of the head of the playlist
{
head -2 "$tempDir/$playList"
echo '#EXT-X-KEY:METHOD=AES-128,URI=''"'"$wKeyPath"'"'
echo '#EXT-X-MEDIA-SEQUENCE:0'
egrep "$tempDir/$playList" -vie '#EXT-X-MEDIA-SEQUENCE:' 
| tail -n +3
} > "$outDir/$playList"

# Get rid of the unencrypted files and unmodified playlist
rm -rf "$tempDir"

# Close the opening if
else exit 2
fi

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读