Skip to content

Instantly share code, notes, and snippets.

@andynu
Created January 3, 2013 21:56
Show Gist options
  • Select an option

  • Save andynu/4447748 to your computer and use it in GitHub Desktop.

Select an option

Save andynu/4447748 to your computer and use it in GitHub Desktop.
Search youtube. Download the first page of videos. Convert to mp3s. requires curl, youtubedown, and soundconverter
#!/usr/bin/env python
from subprocess import check_output, CalledProcessError, call
import re
import glob
# Search youtube
search_term = 'Chordettes'
youtube_search_url = 'https://www.youtube.com/results?search_query=' + search_term
youtube_search_results = check_output(["curl", youtube_search_url])
# Parse video links
matches = re.compile('/watch\?v=[a-zA-Z0-9_\-]{11}').findall(youtube_search_results)
for match in set(matches):
if re.compile('playnext').match(match):
next
video_url = "http://www.youtube.com"+match
# Download Video
try:
print "downloading " + video_url
print check_output(["youtubedown", video_url])
except CalledProcessError as e:
print "ERR: Could not download "+ video_url +" ({0})".format(e)
# Convert to mp3
for file in glob.glob("*.flv") + glob.glob("*.mp4"):
try:
print "Converting "+file+" to mp3"
call(["soundconverter", "-m",'audio/mpeg', "-s",'.mp3', '-b',file])
call(["rm", file])
except CalledProcessError as e:
print "ERR: Could not create mp3 from "+ file +" ({0})".format(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment