Skip to content

Instantly share code, notes, and snippets.

@strayer
Last active March 24, 2021 18:00
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save strayer/7989654 to your computer and use it in GitHub Desktop.
Save strayer/7989654 to your computer and use it in GitHub Desktop.
youtube-dl wrapper script to download DASH Video and Audio and combine it with ffmpeg with automatic best format detection and fallback to default youtube-dl behaviour for videos without DASH
#!/usr/bin/env bash
set -e
YOUTUBE_FORMATS=$(youtube-dl -F "$1")
if [[ "$YOUTUBE_FORMATS" == *"(DASH Video)"* ]]; then
VIDEO_NAME=$(youtube-dl --get-filename "$1")
VIDEO_NAME_TMP="$VIDEO_NAME.tmp"
echo "Filename: $VIDEO_NAME"
if [ -e "$VIDEO_NAME" ]; then
echo "File already exists, aborting..."
exit
fi
# Strip dashes from the beginning of the ID to avoid command line errors
VIDEO_ID=`youtube-dl --get-id "$1" | sed "s/^-/_/"`
VIDEO_FORMAT=`echo "$YOUTUBE_FORMATS" | fgrep "(DASH Video)" | head -n 1`
VIDEO_FORMAT_ID=`echo "$VIDEO_FORMAT" | cut -f 1`
VIDEO_FORMAT_NAME=`echo "$VIDEO_FORMAT" | cut -f 4,5`
AUDIO_FORMAT=`echo "$YOUTUBE_FORMATS" | fgrep "(DASH Audio)" | head -n 1`
AUDIO_FORMAT_ID=`echo "$AUDIO_FORMAT" | cut -f 1`
AUDIO_FORMAT_NAME=`echo "$AUDIO_FORMAT" | cut -f 4,5`
echo "Using formats: $VIDEO_FORMAT_NAME, $AUDIO_FORMAT_NAME"
youtube-dl -f $VIDEO_FORMAT_ID -o "${VIDEO_ID}.vid" "$1"
youtube-dl -f $AUDIO_FORMAT_ID -o "${VIDEO_ID}.aud" "$1"
echo -n "Combining files..."
ffmpeg -i "$VIDEO_ID.vid" -i "$VIDEO_ID.aud" -c copy -f mp4 -v warning \
"$VIDEO_NAME_TMP"
rm "$VIDEO_ID".*
mv "$VIDEO_NAME_TMP" "$VIDEO_NAME"
echo " done!"
else
youtube-dl "$1"
fi
@m3asmi
Copy link

m3asmi commented Feb 23, 2017

@Nikoos can you share it with us ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment