Skip to content

Instantly share code, notes, and snippets.

@ayharano
Created November 18, 2019 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayharano/922060ecb383f03c47e5c6e5b326257a to your computer and use it in GitHub Desktop.
Save ayharano/922060ecb383f03c47e5c6e5b326257a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
INPUT_FILE="${1}"
TIME_START="${2}" # expected format HH:MM:SS
TIME_DURATION="${3}" # expected format HH:MM:SS
OUTPUT_FILE="${4}"
BASE="$(basename "${INPUT_FILE}")"
SHM_AUDIO_FILE="/dev/shm/audio_${BASE}"
SHM_VIDEO_FILE="/dev/shm/video_${BASE}"
SHM_FILE="/dev/shm/${BASE}.mp4"
JSON="/dev/shm/${$}.json"
# Video: cutting, resizing, resampling
ffmpeg \
-i "${INPUT_FILE}" \
-y \
-ss "${TIME_START}" \
-t "${TIME_DURATION}" \
-vcodec h264 \
-vf "scale=1080:-1" \
-an \
"${SHM_VIDEO_FILE}"
# Audio: normalization data
ffmpeg \
-i "${INPUT_FILE}" \
-y \
-ss "${TIME_START}" \
-t "${TIME_DURATION}" \
-vn \
-b:a 256k \
-ar 48000 \
-pass 1 \
-filter:a loudnorm=print_format=json \
-f ac3 \
/dev/null >> "${JSON}" 2>> "${JSON}"
input_i="$(grep 'input_i' "${JSON}" | awk -F':' '{print $2}' | grep -o '".*"' | sed 's,",,g')"
input_tp="$(grep 'input_tp' "${JSON}" | awk -F':' '{print $2}' | grep -o '".*"' | sed 's,",,g')"
input_lra="$(grep 'input_lra' "${JSON}" | awk -F':' '{print $2}' | grep -o '".*"' | sed 's,",,g')"
input_thresh="$(grep 'input_thresh' "${JSON}" | awk -F':' '{print $2}' | grep -o '".*"' | sed 's,",,g')"
ffmpeg \
-i "${INPUT_FILE}" \
-y \
-ss "${TIME_START}" \
-t "${TIME_DURATION}" \
-vn \
-b:a 256k \
-ar 48000 \
-pass 2 \
-af "loudnorm=linear=true:measured_I=${input_i}:measured_LRA=${input_lra}:measured_tp=${input_tp}:measured_thresh=${input_thresh}" \
-acodec ac3 \
"${SHM_AUDIO_FILE}"
ffmpeg \
-i "${SHM_AUDIO_FILE}" \
-i "${SHM_VIDEO_FILE}" \
-c copy \
"${SHM_FILE}"
rsync -avh "${SHM_FILE}" "${OUTPUT_FILE}"
rm "${JSON}"
rm "${SHM_VIDEO_FILE}"
rm "${SHM_AUDIO_FILE}"
rm "${SHM_FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment