Skip to content

Instantly share code, notes, and snippets.

@noboomu
Last active November 7, 2024 01:48
Show Gist options
  • Save noboomu/771871fcb989c31bc231614beb6ce422 to your computer and use it in GitHub Desktop.
Save noboomu/771871fcb989c31bc231614beb6ce422 to your computer and use it in GitHub Desktop.
Generate Multiple Renditions and LL-HLS Streams from Source MP4
#!/bin/bash
# arg 1 is filename arg 2 is output directory
FILENAME=$1
VIDEO_NAME=`echo "${FILENAME%.*}"`
echo "Generating renditions for $VIDEO_NAME"
[ -d "$VIDEO_NAME" ] && rm -rf "$VIDEO_NAME"
mkdir $VIDEO_NAME
cd $VIDEO_NAME
cp ../$1 .
ffmpeg -i $1 \
-filter_complex \
"[0:v]split=3[v1][v2][v3]; \
[v1]scale=w=1280:h=768[v1out]; \
[v2]scale=w=800:h=480[v2out]; \
[v3]scale=w=600:h=360[v3out]" \
-map [v1out] -b:v:0 2800k -maxrate:v:0 2996k -bufsize:v:0 4200k -c:v:0 libx265 -threads 12 -cpu-used 4 -movflags +faststart -preset veryfast -g 48 -sc_threshold 0 \
-map [v2out] -b:v:1 1400k -maxrate:v:1 1498k -bufsize:v:1 2100k -c:v:1 libx265 -threads 12 -cpu-used 4 -movflags +faststart -preset veryfast -g 48 -sc_threshold 0 \
-map [v3out] -b:v:2 800k -maxrate:v:2 898k -bufsize:v:2 900k -c:v:2 libx265 -threads 12 -cpu-used 4 -movflags +faststart -preset veryfast -g 48 -sc_threshold 0 \
-f hls \
-hls_time 2 \
-hls_playlist_type vod \
-hls_segment_type fmp4 \
-hls_flags split_by_time \
-master_pl_name master.m3u8 \
-var_stream_map "v:0 v:1 v:2" \
-hls_segment_filename "rendition_%v/segment_%03d.m4s" \
rendition_%v/playlist.m3u8
echo "Rendition streams generated"
IMAGE_NAME="$VIDEO_NAME"-poster.jpg
echo "image name $IMAGE_NAME"
ffmpeg -i $1 -vf "select=eq(n\,0)" -q:v 2 -frames:v 1 $IMAGE_NAME
jpegoptim -q -m50 $IMAGE_NAME
rm $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment