Skip to content

Instantly share code, notes, and snippets.

@trapexit
Created June 21, 2019 20:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trapexit/355d2483d9b71e5ad433f1366dc72c42 to your computer and use it in GitHub Desktop.
Save trapexit/355d2483d9b71e5ad433f1366dc72c42 to your computer and use it in GitHub Desktop.
Script to preprocess videos for HLS or RTMP streaming
#!/bin/bash
# This is useful when storage is cheaper than compute and you wish to stream
# to later stream to a RTMP target as cheaply as possible.
# To stream
# ffmpeg -re -i <source> -c copy -bsf:a -bsf:a aac_adtstoasc -f flv rtmp://...
RES_X=1920
RES_Y=1080
INPUT=$(realpath "${1}")
TMP_OUTPUT="${INPUT%.*}_TEMP.cfr.ts"
OUTPUT="${INPUT%.*}.cfr.ts"
FFMPEG_INPUT="file:${INPUT}"
FFMPEG_OUTPUT="file:${TMP_OUTPUT}"
ffmpeg \
-y \
-fflags +igndts \
-fflags +genpts \
-fflags +discardcorrupt \
-err_detect ignore_err \
-i "${FFMPEG_INPUT}" \
-c:v libx264 \
-b:v 3500k \
-bsf:v h264_mp4toannexb \
-profile:v high \
-level 4.2 \
-pix_fmt yuv420p \
-preset medium \
-tune film \
-movflags +faststart \
-deinterlace \
-r 30 \
-g 60 \
-vsync cfr \
-c:a aac \
-b:a 192k \
-ac 2 \
-ar 44100 \
-threads 0 \
-crf 21 \
-vf "scale=${RES_X}:${RES_Y}:flags=lanczos:force_original_aspect_ratio=1,pad=${RES_X}:${RES_Y}:(ow-iw)/2:(oh-ih)/2" \
-reset_timestamps 1 \
-f mpegts \
"${FFMPEG_OUTPUT}"
mv -fv "${TMP_OUTPUT}" "${OUTPUT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment