#!/bin/bash


if [ -z "$1" ]; then
  echo "請輸入檔案名稱"
  exit 1
fi

if [ ! -f "$1" ]; then
  echo "file not exists"
  exit 1
fi

FILENAME="${1##*/}"
DIRNAME="${FILENAME%.*}"
EXTNAME="${FILENAME##*.}"

echo "$DIRNAME"

mkdir "$DIRNAME"

# gen key
openssl rand 16 > "$DIRNAME/enc.key"

# new key info file
echo "enc.key" > keyinfo
echo "$DIRNAME/enc.key" >> keyinfo
openssl rand 16 -hex >> keyinfo

# convert
ffmpeg -i "$1" -f hls -hls_time 10 -hls_list_size 0 -vn -hls_segment_filename "$DIRNAME/segment%d.ts" -hls_key_info_file keyinfo -hls_playlist_type vod "$DIRNAME/playlist.m3u8"

# move origin file
cp "$1" "$DIRNAME/origin.$EXTNAME"

# clean tmp file
rm keyinfo

