bash - Encrypted HLS works as live stream, doesn't work as VOD -


i've found crude bash script encoding , encrypting video file, hls stream, , i've edited (i have no idea bash):

#!/bin/bash  set -e     # exit on errors  tsfile="$1"  if ! [ -f "$tsfile" -a -r "$tsfile" ];     echo "first argument required" >&2     exit 2 fi  if [ -z "$3" ];     output="output" else     output="$3" fi   keyfile="$output.key" keyinfofile="$output.keyinfo" playlist="$output.m3u8"  if [ -z "$4" ];     separator='-' else      separator="$4" fi  splitfileprefix="$output$separator"   if [ -d "$2" ];     outdir="$2" else     mkdir "$2" || exit 1     outdir="$2" fi  tempdir="$outdir/.$$_tmp" keyfile="$outdir/$keyfile"  mkdir $tempdir  echo "$outdir/$keyfile\n$outdir/$keyfile" > "$outdir/$keyinfofile"   ffmpeg -i "$tsfile" -hls_time 5 -hls_list_size 0 -hls_segment_filename "$tempdir/$splitfileprefix%03d.ts" -strict -2 "$tempdir/$playlist"  openssl rand 16 > $keyfile encryptionkey=`cat $keyfile | hexdump -e '16/1 "%02x"'`  numberoftsfiles=$(( `ls "$tempdir/$splitfileprefix"*.ts | wc -l` -1 ))  in $(seq -f "%03g" 0 $numberoftsfiles);     initializationvector=`printf '%032x' $(( 10#$i))`     openssl aes-128-cbc -e -in "$tempdir/$splitfileprefix"$i.ts \     -out "$outdir/$splitfileprefix"$i.ts -nosalt -iv $initializationvector -k $encryptionkey done  {     head -4 "$tempdir/$playlist"     echo '#ext-x-key:method=aes-128,uri='"$keyfile"     egrep "$tempdir/$playlist" -vie '#ext-x-targetduration:' \     | tail -n +4 } > "$outdir/$playlist"  #rm -r "$tempdir" 

this results in this:

#extm3u #ext-x-version:3 #ext-x-media-sequence:0 #ext-x-key:method=aes-128,uri=output.key #ext-x-targetduration:11 #extinf:10.176833, output-000.ts #extinf:8.341667, output-001.ts #extinf:8.341667, output-002.ts #extinf:3.136467, output-003.ts #ext-x-endlist 

this works. need vod, not live stream. so, added line:

#ext-x-playlist-type:vod 

and doesn't work encrypted segments, unencrypted ones. thought segments crypted separately? also, unencrypted files, info total length isn't present. how can fix that?

here few pointers based on own experiments seem work on vlc, ios , android.

  • initialization vectors

    when no iv specified in playlist each segment has default iv equal media sequence. make sure segment-000 has iv=0, segment-001 has iv=1 , on.

  • quoting uri

    ios doesn't seem playlist uri doesn't use quotes use ext-x-key:method=aes-128,uri="output.key"

  • playlist type vod

    ext-x-playlist-type optional and, long have ext-x-endlist @ end, playlist treated static , allows seek. or without tag both vlc , ios treat playlist vod.

    concerning media duration, vlc shows 0 wile ios shows correct value.

    if do specify ext-x-playlist-type:vod make sure comes after ext-x-version:3 or vlc won't it.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -