ffmpeg切割TS脚本带加密

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/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