Skip to content

Instantly share code, notes, and snippets.

@gaurav22verma
Created April 7, 2019 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gaurav22verma/d1dd9cdb9527e1198244d66120afab7a to your computer and use it in GitHub Desktop.
Save gaurav22verma/d1dd9cdb9527e1198244d66120afab7a to your computer and use it in GitHub Desktop.
A sample Python script to download audio from YouTube URL
import pafy
import time
sample_URLs = ['wdPAIXBmcF0', 'FiiA9OSm5lc']
for an_elt in sample_URLs:
URL = an_elt.strip()
try:
video = pafy.new(URL)
# Check that the duration of the video is less than 10 minutes
duration = video.duration.split(':')
if int(duration[0]) == 0 and int(duration[1]) <= 10:
bestaudio = video.getbestaudio()
Title = str(video.title)
bestaudio.download(filepath = './')
else:
continue
except:
print "Failed!"
time.sleep(21)
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment