-
-
Save dlobjoie/221d5a898204fa5a4bded5bf334d0a7f to your computer and use it in GitHub Desktop.
Bash scripts to create VOD HLS stream with ffmpeg (Extended version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
START_TIME=$SECONDS | |
set -e | |
echo "-----START GENERATING HLS STREAM-----" | |
# Usage create-vod-hls.sh SOURCE_FILE [OUTPUT_NAME] | |
[[ ! "${1}" ]] && echo "Usage: create-vod-hls.sh SOURCE_FILE [OUTPUT_NAME]" && exit 1 | |
# commenter/ajouter des lignes ici pour contrôler les rendus qui seront créés | |
renditions=( | |
# resolution bitrate audio-rate | |
"426x240 400k 128k" | |
"640x360 800k 128k" | |
"842x480 1400k 192k" | |
"1280x720 2800k 192k" | |
"1920x1080 5000k 256k" | |
) | |
segment_target_duration=5 # essayer de créer un nouveau segment toutes les 5 secondes | |
max_bitrate_ratio=1.07 # fluctuations maximales acceptées du débit binaire | |
rate_monitor_buffer_ratio=1.5 # taille maximale du tampon entre les contrôles de conformité du débit binaire | |
######################################################################### | |
source="${1}" | |
target="${2}" | |
if [[ ! "${target}" ]]; then | |
target="${source##*/}" # ne laisser que le dernier composant du chemin | |
target="${target%.*}" # strip extension | |
fi | |
mkdir -p ${target} | |
# ----PERSONNALISATION---- | |
sourceResolution="$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 ${source})" | |
# echo ${sourceResolution} | |
arrIN=(${sourceResolution//x/ }) | |
sourceWidth="${arrIN[0]}" | |
sourceHeight="${arrIN[1]}" | |
echo ${sourceWidth} | |
echo ${sourceHeight} | |
sourceAudioBitRate="$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of csv=s=x:p=0 ${source})" | |
sourceAudioBitRateFormatted=$((sourceAudioBitRate / 1000)) | |
# ----FIN PERSONNALISATION---- | |
key_frames_interval="$(echo `ffprobe ${source} 2>&1 | grep -oE '[[:digit:]]+(.[[:digit:]]+)? fps' | grep -oE '[[:digit:]]+(.[[:digit:]]+)?'`*2 | bc || echo '')" | |
key_frames_interval=${key_frames_interval:-50} | |
key_frames_interval=$(echo `printf "%.1f\n" $(bc -l <<<"$key_frames_interval/10")`*10 | bc) # round | |
key_frames_interval=${key_frames_interval%.*} # truncate to integer | |
# Paramètres statiques qui sont similaires pour tous les rendus | |
static_params="-c:a aac -ar 48000 -c:v libx265 -profile:v main -crf 19 -sc_threshold 0" | |
static_params+=" -g ${key_frames_interval} -keyint_min ${key_frames_interval} -hls_time ${segment_target_duration}" | |
static_params+=" -hls_playlist_type vod" | |
# Paramètres divers | |
misc_params="-hide_banner -y" | |
master_playlist="#EXTM3U | |
#EXT-X-VERSION:3 | |
" | |
cmd="" | |
resolutionValid=0 | |
prevHeight=0 | |
for rendition in "${renditions[@]}"; do | |
# Suppression des espaces superflus | |
rendition="${rendition/[[:space:]]+/ }" | |
# Champs de rendu | |
resolution="$(echo ${rendition} | cut -d ' ' -f 1)" | |
bitrate="$(echo ${rendition} | cut -d ' ' -f 2)" | |
audiorate="$(echo ${rendition} | cut -d ' ' -f 3)" | |
audioBitRateFormatted=${audiorate%?} # Suppression du "k" dans le dernier index | |
# adoption du débit binaire audio le plus élevé possible | |
if [ $audioBitRateFormatted -gt $sourceAudioBitRateFormatted ]; then | |
audiorate=${sourceAudioBitRateFormatted}k | |
fi | |
# calculated fields | |
width="$(echo ${resolution} | grep -oE '^[[:digit:]]+')" | |
height="$(echo ${resolution} | grep -oE '[[:digit:]]+$')" | |
maxrate="$(echo "`echo ${bitrate} | grep -oE '[[:digit:]]+'`*${max_bitrate_ratio}" | bc)" | |
bufsize="$(echo "`echo ${bitrate} | grep -oE '[[:digit:]]+'`*${rate_monitor_buffer_ratio}" | bc)" | |
bandwidth="$(echo ${bitrate} | grep -oE '[[:digit:]]+')000" | |
name="${height}p" | |
if [ $sourceHeight -le $prevHeight ]; then | |
echo "video source has height smaller than output height (${height})" | |
break | |
fi | |
widthParam=0 | |
heightParam=0 | |
if [ $(((width / sourceWidth) * sourceHeight)) -gt $height ]; then | |
widthParam=-2 | |
heightParam=$height | |
else | |
widthParam=$width | |
heightParam=-2 | |
fi | |
cmd+=" ${static_params} -vf scale=w=${widthParam}:h=${heightParam}" | |
cmd+=" -b:v ${bitrate} -maxrate ${maxrate%.*}k -bufsize ${bufsize%.*}k -b:a ${audiorate}" | |
cmd+=" -hls_segment_filename ${target}/${name}_%03d.ts ${target}/${name}.m3u8" | |
# Ajout d'une entrée d'interprétation dans la liste de lecture principale | |
master_playlist+="#EXT-X-STREAM-INF:BANDWIDTH=${bandwidth},RESOLUTION=${resolution}\n${name}.m3u8\n" | |
resolutionValid=1 | |
prevHeight=${height} | |
done | |
if [ $resolutionValid -eq 1 ]; then | |
# Début de la conversion | |
echo -e "Executing command:\nffmpeg ${misc_params} -i ${source} ${cmd}\n" | |
ffmpeg ${misc_params} -i ${source} ${cmd} | |
# Création d'un fichier de liste de lecture principal | |
echo -e "${master_playlist}" > ${target}/playlist.m3u8 | |
echo "Done - encoded HLS is at ${target}/" | |
else | |
echo "Video source is too small" | |
exit 1 | |
fi | |
ELAPSED_TIME=$(($SECONDS - $START_TIME)) | |
echo "Elapsed time: ${ELAPSED_TIME}" | |
echo "-----FINISH GENERATING HLS STREAM-----" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment