-
-
Save Rafat97/3d38bc6c150b0f809650f91258aad06b to your computer and use it in GitHub Desktop.
๐ผ FFMPEG Some Command ๐ผ
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
#!/bin/bash | |
# bash create-vod-dash.sh test.mp4 | |
MYDIR=$(dirname $(readlink -f ${BASH_SOURCE[0]})) | |
SAVEDIR=$(pwd) | |
# Controlla che i programmi richiesti siano installati | |
if [ -z "$(which ffmpeg)" ]; then | |
echo "Errore: ffmpeg non e' installato" | |
exit 1 | |
fi | |
cd "$MYDIR" | |
TARGET_FILES=$(find ./ -type f -name "*.mp4") | |
for f in $TARGET_FILES | |
do | |
f=$(basename "$f") # memorizza il nome completo del file | |
f="${f%.*}" # toglie l'estensione | |
if [ ! -f "${f}.mpd" ]; then | |
echo "Converto il file \"$f\" in Adaptive WebM using DASH" | |
echo "Riferimenti: http://wiki.webmproject.org/adaptive-streaming/instructions-to-playback-adaptive-webm-using-dash" | |
# http://wiki.webmproject.org/adaptive-streaming/instructions-to-playback-adaptive-webm-using-dash | |
VP9_DASH_PARAMS="-tile-columns 4 -frame-parallel 1" | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:90 -b:v 250k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:90 -b:v 250k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_160px_250k.webm" | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:180 -b:v 500k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:180 -b:v 500k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_320px_500k.webm" | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:360 -b:v 750k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:360 -b:v 750k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_640px_750k.webm" | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:360 -b:v 1000k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:360 -b:v 1000k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_640px_1000k.webm" | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:720 -b:v 1500k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 1 -y /dev/null | |
ffmpeg -i "${f}.mp4" -c:v libvpx-vp9 -vf scale=-1:720 -b:v 1500k -keyint_min 150 -g 150 ${VP9_DASH_PARAMS} -an -f webm -dash 1 -pass 2 "${f}_1280px_1500k.webm" | |
ffmpeg -i "${f}.mp4" -c:a libvorbis -b:a 128k -vn -f webm -dash 1 "${f}_audio_128k.webm" | |
rm -f ffmpeg*.log | |
ffmpeg \ | |
-f webm_dash_manifest -i "${f}_160px_250k.webm" \ | |
-f webm_dash_manifest -i "${f}_320px_500k.webm" \ | |
-f webm_dash_manifest -i "${f}_640px_750k.webm" \ | |
-f webm_dash_manifest -i "${f}_640px_1000k.webm" \ | |
-f webm_dash_manifest -i "${f}_1280px_1500k.webm" \ | |
-f webm_dash_manifest -i "${f}_audio_128k.webm" \ | |
-c copy -map 0 -map 1 -map 2 -map 3 -map 4 -map 5 \ | |
-f webm_dash_manifest \ | |
-adaptation_sets "id=0,streams=0,1,2,3,4 id=1,streams=5" \ | |
"${f}.mpd" | |
fi | |
done | |
cd "$SAVEDIR" |
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 | |
# bash create-vod-hls.sh beach.mkv | |
# http://docs.peer5.com/guides/production-ready-hls-vod/ | |
set -e | |
# Usage create-vod-hls.sh SOURCE_FILE [OUTPUT_NAME] | |
[[ ! "${1}" ]] && echo "Usage: create-vod-hls.sh SOURCE_FILE [OUTPUT_NAME]" && exit 1 | |
# comment/add lines here to control which renditions would be created | |
renditions=( | |
# resolution bitrate audio-rate | |
"426x240 400k 64k" | |
"640x360 800k 96k" | |
"842x480 1400k 128k" | |
"1280x720 2800k 128k" | |
"1920x1080 5000k 192k" | |
) | |
segment_target_duration=4 # try to create a new segment every X seconds | |
max_bitrate_ratio=1.07 # maximum accepted bitrate fluctuations | |
rate_monitor_buffer_ratio=1.5 # maximum buffer size between bitrate conformance checks | |
######################################################################### | |
source="${1}" | |
target="${2}" | |
if [[ ! "${target}" ]]; then | |
target="${source##*/}" # leave only last component of path | |
target="${target%.*}" # strip extension | |
fi | |
mkdir -p ${target} | |
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 | |
# static parameters that are similar for all renditions | |
static_params="-c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -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" | |
# misc params | |
misc_params="-hide_banner -y" | |
master_playlist="#EXTM3U | |
#EXT-X-VERSION:3 | |
" | |
cmd="" | |
for rendition in "${renditions[@]}"; do | |
# drop extraneous spaces | |
rendition="${rendition/[[:space:]]+/ }" | |
# rendition fields | |
resolution="$(echo ${rendition} | cut -d ' ' -f 1)" | |
bitrate="$(echo ${rendition} | cut -d ' ' -f 2)" | |
audiorate="$(echo ${rendition} | cut -d ' ' -f 3)" | |
# 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" | |
cmd+=" ${static_params} -vf scale=w=${width}:h=${height}:force_original_aspect_ratio=decrease" | |
cmd+=" -b:v ${bitrate} -maxrate ${maxrate%.*}k -bufsize ${bufsize%.*}k -b:a ${audiorate}" | |
cmd+=" -hls_segment_filename ${target}/${name}_%03d.ts ${target}/${name}.m3u8" | |
# add rendition entry in the master playlist | |
master_playlist+="#EXT-X-STREAM-INF:BANDWIDTH=${bandwidth},RESOLUTION=${resolution}\n${name}.m3u8\n" | |
done | |
# start conversion | |
echo -e "Executing command:\nffmpeg ${misc_params} -i ${source} ${cmd}" | |
ffmpeg ${misc_params} -i ${source} ${cmd} | |
# create master playlist file | |
echo -e "${master_playlist}" > ${target}/playlist.m3u8 | |
echo "Done - encoded HLS is at ${target}/" |
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
This is tested on Docker image `jrottenberg/ffmpeg:4.1-alpine` | |
/********************************** | |
* Change resolution | |
************************************/ | |
ffmpeg -y -i <input> \ | |
-vf scale=256:144 output_144p.mp4 \ | |
-vf scale=352:240 output_240p.mp4 \ | |
-vf scale=480:360 output_360p.mp4 \ | |
-vf scale=858:480 output_480p.mp4 \ | |
-vf scale=1280:720 output_720p.mp4 \ | |
-vf scale=1920:1080 output_1080p.mp4 \ | |
-vf scale=2560:1440 output_1440p.mp4 \ | |
-vf scale=3840:2160 output_2160p.mp4 | |
/********************************** | |
* Create HLS | |
************************************/ | |
ffmpeg -y -i <input> \ | |
-vf scale=w=640:h=360:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -hls_segment_filename beach/360p_%03d.ts beach/360p.m3u8 \ | |
-vf scale=w=842:h=480:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 1400k -maxrate 1498k -bufsize 2100k -b:a 128k -hls_segment_filename beach/480p_%03d.ts beach/480p.m3u8 \ | |
-vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 2800k -maxrate 2996k -bufsize 4200k -b:a 128k -hls_segment_filename beach/720p_%03d.ts beach/720p.m3u8 \ | |
-vf scale=w=1920:h=1080:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 5000k -maxrate 5350k -bufsize 7500k -b:a 192k -hls_segment_filename beach/1080p_%03d.ts beach/1080p.m3u8 | |
/********************************** | |
* Create HLS+DASH | |
************************************/ | |
ffmpeg -re -i <input> \ | |
-map 0 -b:v:0 350k -c:v:0 libx264 -c:a:0 libfdk_aac -s:v:0 320x170 -profile:v:0 baseline \ | |
-map 0 -b:v:1 1000k -c:v:1 libx264 -c:a:1 libfdk_aac -s:v:1 640x360 -profile:v:1 main \ | |
-map 0 -b:v:2 2800k -c:v:2 libx264 -c:a:2 libfdk_aac -filter:v:2 "scale=1280x720" -profile:v:2 high \ | |
-bf 1 -keyint_min 120 -g 120 -sc_threshold 0 -b_strategy 0 \ | |
-bufsize 1400k -use_timeline 1 -use_template 1 -adaptation_sets "id=0,streams=v id=1,streams=a" \ | |
-hls_playlist true \ | |
-f dash dash/test.mpd | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment