Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save andrewstellman/2f47ec634755652252446a3ff45c7218 to your computer and use it in GitHub Desktop.

Select an option

Save andrewstellman/2f47ec634755652252446a3ff45c7218 to your computer and use it in GitHub Desktop.
prompt - what does this shell script do
what does this shell script do
#!/bin/bash
# Usage: ./replace_audio.sh input.mov
set -e
if [ $# -ne 1 ]; then
echo "Usage: $0 <video.mov>"
exit 1
fi
VIDEO="$1"
BASENAME="${VIDEO%.}"
EXT="${VIDEO##.}"
AUDIO_FILE="${BASENAME}-audio.wav"
FINAL_VIDEO="${BASENAME} final.${EXT}"
# --- Step 1: Verify original audio format ---
echo "Verifying original audio format..."
ORIG_INFO=$(ffprobe -v error -select_streams a:0 \
-show_entries stream=codec_name,bits_per_sample,sample_rate \
-of default=noprint_wrappers=1:nokey=1 "$VIDEO")
ORIG_CODEC=$(echo "$ORIG_INFO" | sed -n 1p)
ORIG_BITS=$(echo "$ORIG_INFO" | sed -n 2p)
ORIG_RATE=$(echo "$ORIG_INFO" | sed -n 3p)
if [[ "$ORIG_CODEC" != "alac" ]]; then
echo "❌ Error: Original audio is not Apple Lossless (ALAC). Found: $ORIG_CODEC"
exit 1
fi
if [[ "$ORIG_BITS" -ne 24 ]]; then
echo "❌ Error: Original audio is not 24-bit. Found: $ORIG_BITS"
exit 1
fi
echo "✅ Original audio format verified: ALAC, ${ORIG_BITS}-bit, ${ORIG_RATE} Hz"
# --- Step 2: Extract audio for RX editing ---
echo "Extracting audio to $AUDIO_FILE..."
ffmpeg -i "$VIDEO" -vn -acodec pcm_s24le "$AUDIO_FILE"
echo "Audio extracted. Open $AUDIO_FILE in RX, make edits, and overwrite it with the same name."
read -p "Press Enter to continue after editing in RX..."
# --- Step 3: Replace audio in the original video ---
echo "Replacing audio..."
ffmpeg -i "$VIDEO" -i "$AUDIO_FILE" -map 0:v -map 1:a -c:v copy -c:a alac "$FINAL_VIDEO"
# --- Step 4: Verify final audio format ---
echo "Verifying final audio format..."
FINAL_INFO=$(ffprobe -v error -select_streams a:0 \
-show_entries stream=codec_name,bits_per_sample,sample_rate \
-of default=noprint_wrappers=1:nokey=1 "$FINAL_VIDEO")
FINAL_CODEC=$(echo "$FINAL_INFO" | sed -n 1p)
FINAL_BITS=$(echo "$FINAL_INFO" | sed -n 2p)
FINAL_RATE=$(echo "$FINAL_INFO" | sed -n 3p)
if [[ "$FINAL_CODEC" != "alac" ]]; then
echo "❌ Error: Final audio is not Apple Lossless (ALAC). Found: $FINAL_CODEC"
exit 1
fi
if [[ "$FINAL_BITS" -ne 24 ]]; then
echo "❌ Error: Final audio is not 24-bit. Found: $FINAL_BITS"
exit 1
fi
if [[ "$FINAL_RATE" -ne "$ORIG_RATE" ]]; then
echo "❌ Error: Sample rate changed. Original: $ORIG_RATE, Final: $FINAL_RATE"
exit 1
fi
echo "✅ Final audio format verified: ALAC, ${FINAL_BITS}-bit, ${FINAL_RATE} Hz"
echo "🎉 Done! Final video saved as: $FINAL_VIDEO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment