Skip to content

Instantly share code, notes, and snippets.

@ashish1405
Last active May 7, 2021 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashish1405/c06374d55d863f6f400205fd814b9964 to your computer and use it in GitHub Desktop.
Save ashish1405/c06374d55d863f6f400205fd814b9964 to your computer and use it in GitHub Desktop.
Shell script for generating m3u8 file from a list of encrypted ts files.
#!/bin/bash
keyfilepath=$(dirname "${1}")
#read keyinfo file
IFS=$'\n' read -d '' -r -a keyfilelines < $1
KEYFILE="$keyfilepath/${keyfilelines[1]}"
IV=${keyfilelines[2]}
KEY=$(xxd -p $KEYFILE)
#read m3u8 file
IFS=$'\n' read -d '' -r -a m3u8filelines < $2
#write header
for f in ${m3u8filelines[*]};
do
if [[ $f == *"#EXTINF"* ]]; then
break
fi
echo $f >> $4
done
#read ts file list
IFS=$'\n' read -d '' -r -a tsfilelines < $3
for f in ${tsfilelines[*]};
do
duration=`ffprobe -key $KEY -iv $IV -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 crypto:$f`
#write duration for x file
echo "#EXTINF:$duration">>$4
#write name of x file
echo "$f">>$4
done
echo "#EXT-X-ENDLIST">>$4
ffmpeg -i $1 -hide_banner -loglevel panic -codec: copy -start_number 0 -hls_time 6 -hls_key_info_file enc.keyinfo -hls_list_size 2 -f hls filename.m3u8
@ashish1405
Copy link
Author

The encrypted ts files are generated using ffmpeg command (record.sh).
The main script takes 4 parameters

  1. Path to enc.keyinfo file with full path
  2. Path to original m3u8 file to take the header, if you don't have this you can create your own m3u8 header
  3. Path to file which lists all ts files with full path, which needs to be included in m3u8
  4. New m3u8 file name with path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment