Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Zehkul
Last active August 29, 2015 14:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zehkul/cb60ea8bf5e12b4eb2fc to your computer and use it in GitHub Desktop.
Save Zehkul/cb60ea8bf5e12b4eb2fc to your computer and use it in GitHub Desktop.
Dirty script to play 1080p + 480p from youtube (DASH) in mpv
#!/bin/bash
#
# Dirty script to play 1080p + 480p from youtube (DASH) in mpv
# --audio-file adjusted for mpv 0.4 (0.3: --audiofile)
#
video_url="$1"
youtube_dash_video_formats="137/136/135/134/133"
youtube_dash_audio_formats="172/141/171/140/139"
cache_settings="--cache=1000000"
youtube_dash () {
title_and_video=$(youtube-dl -g -e -f "$youtube_dash_video_formats" "$video_url")
audio=$(youtube-dl -g -f "$youtube_dash_audio_formats" "$video_url")
mpv $(echo "$title_and_video" | sed -n 2p) \
--title="$(echo "$title_and_video" | sed -n 1p)" \
--audio-file $audio \
--demuxer-lavf-o=fflags=+ignidx \
$cache_settings
}
normal_playback () {
title_and_video=$(youtube-dl -g -e $video_url)
mpv $(echo "$title_and_video" | sed -n 2p) \
--title="$(echo "$title_and_video" | sed -n 1p)" \
$cache_settings
}
if [[ "$1" =~ "www.youtube.com/" ]]; then
formats=$(youtube-dl -F $1)
if [[ "$formats" =~ "1080" ]]; then
echo "Playing: 1080p DASH"
youtube_dash
elif [[ "$formats" =~ "720" ]]; then
echo "Playing: 720p"
normal_playback
elif [[ "$formats" =~ "480" ]]; then
echo "Playing: 480p DASH"
youtube_dash
else
echo "Playing: 360p/240p"
normal_playback
fi
else
normal_playback
fi

You probably shouldn’t be using this any more, mpv as of version 0.7 can directly make use of youtube-dl with the --ytdl option (enabled by default in 0.7.2) You can configure what formats mpv will play with the --ytdl-format option, something similar to this script can be achieved with --ytdl-format=303+bestaudio[ext=webm]/299+bestaudio/248+bestaudio[ext=webm]/137+bestaudio/302+bestaudio[ext=webm]/298+bestaudio/22/244+bestaudio[ext=webm]/135+bestaudio/18/best

This will also prefer 60fps content over normal versions of the videos and vp9 over h264, see https://en.wikipedia.org/wiki/Youtube#Quality_and_formats

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