79 lines
2.8 KiB
Bash
79 lines
2.8 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Prepare the videos to be used over http with the MPEG-DASH protocol
|
|
# The vp9 codec is used for the videos because h265 is not supported by chrome and h264 is too big
|
|
# This script generates webm files
|
|
|
|
## DOCS
|
|
# https://developers.google.com/media/vp9/settings/vod/
|
|
# https://developer.mozilla.org/en-US/docs/Web/HTML/DASH_Adaptive_Streaming_for_HTML_5_Video
|
|
# http://wiki.webmproject.org/adaptive-streaming/instructions-to-playback-adaptive-webm-using-dash
|
|
|
|
# ffprobe: https://trac.ffmpeg.org/wiki/FFprobeTips
|
|
|
|
## PRE-REQUIRED-INSTALLATION : ffmpeg and libwebm
|
|
# OSX: brew install ffmpeg --with-libvpx --with-libvorbis
|
|
# Debian:
|
|
|
|
set -eo pipefail
|
|
|
|
INPUT=$1
|
|
OUTPUT_DIR=$2
|
|
DASH_PARAMS="-keyint_min 150 -g 150 -an -f webm -dash 1"
|
|
LOG_LEVEL="-hide_banner -loglevel info"
|
|
|
|
# Checks
|
|
if [[ -z $(which ffmpeg) ]]; then echo "Error: ffmpeg is not installed"; exit 1; fi
|
|
if [[ $# -ne 2 ]]; then echo "Usage: mp4_to_dash input_file.mp4 output_directory"; exit 1; fi
|
|
if [[ ! -f ${INPUT} ]]; then echo "Error: input_file not found"; exit 1; fi
|
|
if [[ ! -d ${OUTPUT_DIR} ]]; then echo "Error: output_directory not found"; exit 1; fi
|
|
|
|
# create new directory using the name of the video
|
|
f=$(basename $INPUT)
|
|
f=${f%.*} # name without extension, and without base
|
|
rm -rf ${OUTPUT_DIR}/${f}
|
|
mkdir -p ${OUTPUT_DIR}/${f}
|
|
prefix=${OUTPUT_DIR}/${f}/${f}
|
|
|
|
encode_audio() {
|
|
ffmpeg $LOG_LEVEL -i "$INPUT" -c:a libvorbis -b:a 128k -vn -f webm -dash 1 -y "${prefix}-audio-128k.webm"
|
|
}
|
|
|
|
getHeight() {
|
|
ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 "$INPUT"
|
|
}
|
|
|
|
encode(){
|
|
size_params=$1
|
|
output=$2
|
|
echo "ffmpeg $LOG_LEVEL -i $INPUT -c:v libvpx-vp9 $size_params $DASH_PARAMS -pass 1 -speed 4 -y $output"
|
|
ffmpeg $LOG_LEVEL -i $INPUT -c:v libvpx-vp9 $size_params $DASH_PARAMS -pass 1 -speed 4 -y $output && \
|
|
ffmpeg $LOG_LEVEL -i $INPUT -c:v libvpx-vp9 $size_params $DASH_PARAMS -pass 2 -speed 4 -y $output
|
|
}
|
|
|
|
encode_240p() {
|
|
size="320x240"
|
|
size_params="-s ${size} -b:v 150k -minrate 75k -maxrate 218k -tile-columns 0 -threads 2 -quality good -crf 37"
|
|
encode "$size_params" "${prefix}-${size}.webm"
|
|
}
|
|
|
|
encode_360p() {
|
|
size="640x360"
|
|
size_params="-s ${size} -b:v 276k -minrate 138k -maxrate 400k -tile-columns 1 -threads 4 -quality good -crf 36"
|
|
encode "$size_params" "${prefix}-${size}.webm"
|
|
}
|
|
|
|
encode_720p() {
|
|
size="1280x720"
|
|
size_params_30f="-s ${size} -b:v 1024k -minrate 512k -maxrate 1485k -tile-columns 2 -threads 8 -quality good -crf 32"
|
|
encode "$size_params_30f" "${prefix}-${size}-30f.webm"
|
|
|
|
size_params_60f="-s ${size} -b:v 1800k -minrate 900k -maxrate 2610k -tile-columns 2 -threads 8 -quality good -crf 32"
|
|
encode "$size_params_60f" "${prefix}-${size}-60f.webm"
|
|
}
|
|
|
|
video_original_height=$(getHeight)
|
|
encode_audio
|
|
encode_360p
|
|
if [[ "$video_original_height" -ge "720" ]]; then encode_720p; fi
|