Skip to content

Instantly share code, notes, and snippets.

@icosane
Forked from nneonneo/youtube-dl.py
Last active April 23, 2022 01:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icosane/0737960d0a5102d03c6e5a46826f0168 to your computer and use it in GitHub Desktop.
Save icosane/0737960d0a5102d03c6e5a46826f0168 to your computer and use it in GitHub Desktop.
yt-dlp for Pythonista - download or stream YouTube videos on your iPhone/iPad
#!python3
'''
As of 17/10/21, probably not working and with no fix in sight. Youtube changed something on their side, so yt-dlp and youtube-dl fail to extract videos. Only format 22, which is 720p, works. I haven't used it on my phone for a few months, but yt-dlp loads only 720p on my PC, so I think it is the same for this script.
Directions:
- install yt-dlp via pip (e.g. using the StaSh command: https://github.com/ywangd/stash)
- add this script as a Share extension through Settings -> Share Extension Shortcuts
- while watching a video in the YouTube site or app, just share the video to Pythonista and select this script
- the video will download, and when it's done you can share the video file itself with any app (e.g. VLC)
Advanced usage:
- if you specify --stream as the script argument, this script will just grab the actual video URL and redirect you
to VLC/Safari/Outplayer, which will stream the video (without interruptions or ads!)
(see how to change which app to use below)
'''
from __future__ import unicode_literals
import yt_dlp
import appex
import console
import clipboard
import os
import sys
outdir = os.path.expanduser("~/Documents/Downloads")
try:
os.mkdir(outdir)
except FileExistsError:
pass
if appex.get_attachments():
# e.g. share from YouTube app
url = appex.get_attachments()[0]
elif appex.get_urls():
# e.g. share from Safari
url = appex.get_urls()[0]
elif appex.get_text():
url = appex.get_text()
elif clipboard.get():
url = clipboard.get()
print("URL: ", url)
if not url or not url.startswith("http"):
url = input("No URL found - enter URL to download: ")
ydl_opts = {'outtmpl': os.path.join(outdir, '%(title)s.%(ext)s')}
if sys.argv[1:] == ['--stream']:
'''
#for 720p, but streams do not work
ydl_opts = {
'format': '22'
}
'''
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
from objc_util import UIApplication, nsurl
from urllib.parse import urlencode
app = UIApplication.sharedApplication()
urls = [f['url'] for f in info['formats'] if f['acodec'] != 'none' and f['vcodec'] != 'none']
params = urlencode({'url': urls[-1]})
'''
Video streaming by default will open in Outplayer
Uncomment the first line to play the video in VLC, the second line to stream in Safari, or the third line to stream in Outplayer (in Outplayer some videos start from the middle smh)
'''
#app.openURL_(nsurl('vlc-x-callback://x-callback-url/stream?' + params))
#app.openURL_(nsurl(urls[-1]))
app.openURL_(nsurl('outplayer://' + urls[-1]))
os.abort()
else:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
filepath = ydl.prepare_filename(info)
console.open_in(filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment