Skip to content

Instantly share code, notes, and snippets.

@jadedgnome
Forked from strayer/youtube-dl-dash.bash
Last active May 19, 2018 01:12
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 jadedgnome/dca265332f4d0f41ec8fa3e308c0032a to your computer and use it in GitHub Desktop.
Save jadedgnome/dca265332f4d0f41ec8fa3e308c0032a 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" | grep "137\|136" | head -n 1`
VIDEO_FORMAT_ID=`echo "$VIDEO_FORMAT" | cut -d ' ' -f 1`
VIDEO_FORMAT_NAME=`echo "$VIDEO_FORMAT" | cut -f 4,5`
AUDIO_FORMAT=`echo "$YOUTUBE_FORMATS" | fgrep "audio only" | sort -r | 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment