Skip to content

Instantly share code, notes, and snippets.

@SteveClement
Created April 16, 2020 05:45
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 SteveClement/ca71f0eb99b119e2b5664f8825cda89d to your computer and use it in GitHub Desktop.
Save SteveClement/ca71f0eb99b119e2b5664f8825cda89d to your computer and use it in GitHub Desktop.
Explode video file with ffmpeg
#!/usr/bin/env bash
INPUT_FILE="${1}"
if [[ ! -f "${1}" ]]; then
echo "File "${1}" does not exist"
exit -1
fi
if [[ ${INPUT_FILE} == *"/"* ]]; then
echo "Slash exists"
INPUT_FN=$(echo ${INPUT_FILE} |cut -f 2 -d/)
else
INPUT_FN=${INPUT_FILE}
fi
OUTPUT_DIR="output"
if [[ ! -d ${OUTPUT_DIR} ]]; then
mkdir ${OUTPUT_DIR}
fi
OUTPUT_FILE="${OUTPUT_DIR}/$(echo ${INPUT_FN} |cut -f 1 -d\.)"
AUDIO_TRACKS=$(ffprobe -v quiet -print_format csv -show_format -show_streams ${INPUT_FILE} |grep stream |grep audio| wc -l)
AUDIO_EXT=$(ffprobe -v quiet -print_format csv -show_format -show_streams ${INPUT_FILE} |grep stream |grep audio| head -1| cut -f3 -d,)
VIDEO_EXT=$(echo ${1} |cut -f 2 -d\.)
VIDEO_TRACKS=$(ffprobe -v quiet -print_format csv -show_format -show_streams ${INPUT_FILE} |grep stream |grep video| wc -l)
echo "${INPUT_FILE} has ${AUDIO_TRACKS} track(s) and ${VIDEO_TRACKS} track(s)"
ffmpeg -i ${INPUT_FILE} \
-map "0:v:0?" -c copy ${OUTPUT_FILE}_video0.${VIDEO_EXT} \
-map "0:v:1?" -c copy ${OUTPUT_FILE}_video1.${VIDEO_EXT} \
-map "0:v:2?" -c copy ${OUTPUT_FILE}_video2.${VIDEO_EXT} \
-map "0:v:3?" -c copy ${OUTPUT_FILE}_video3.${VIDEO_EXT} \
-map "0:a:0?" -c copy ${OUTPUT_FILE}_audio0.${AUDIO_EXT} \
-map "0:a:1?" -c copy ${OUTPUT_FILE}_audio1.${AUDIO_EXT} \
-map "0:a:2?" -c copy ${OUTPUT_FILE}_audio2.${AUDIO_EXT} \
-map "0:a:3?" -c copy ${OUTPUT_FILE}_audio3.${AUDIO_EXT} \
-map "0:a:4?" -c copy ${OUTPUT_FILE}_audio4.${AUDIO_EXT} \
-map "0:a:5?" -c copy ${OUTPUT_FILE}_audio5.${AUDIO_EXT} \
-map "0:a:6?" -c copy ${OUTPUT_FILE}_audio6.${AUDIO_EXT} \
-map "0:a:7?" -c copy ${OUTPUT_FILE}_audio7.${AUDIO_EXT} \
-map "0:a:8?" -c copy ${OUTPUT_FILE}_audio8.${AUDIO_EXT} \
-map "0:a:9?" -c copy ${OUTPUT_FILE}_audio9.${AUDIO_EXT}
# TODO: check if fdupes exists
fdupes -d -N -I -p output/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment