-
-
Save kikohs/78898124c9661889817d354da06f1213 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment