Skip to content

Instantly share code, notes, and snippets.

@MicahElliott
Created November 29, 2010 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MicahElliott/720410 to your computer and use it in GitHub Desktop.
Save MicahElliott/720410 to your computer and use it in GitHub Desktop.
Download and Convert youtube "favorite" videos to MP3.
#! /usr/bin/env python
""" youtube2mp3 - Download and Convert youtube "favorite" videos to MP3.
Determine "favorited" vids, download them as FLVs, extract/ save its
MP3, meanwhile fixing up file names.
Assumes most (or all) of the things you "favorite" on Youtube are some
sort of musical recording. I've found a lot of old jazz tunes on Youtube
that I can't otherwise find recordings for. Down side is that the
quality of the MP3s is usually mediocre at best.
This could use a lot of improvement, but works okay for common cases as-is.
Demonstrates simple usage of Youtube API and conversion/extraction with
clive/ffmpeg.
Dependencies:
libgdata (apt)
clive (apt, almost)
fnfix (https://gist.github.com/719728)
Note that clive on Ubuntu (lucid) is not working, so need to get an
updated version, due to YT's recent changes.
"""
__author__ = 'Micah Elliott http://MicahElliott.com'
__version__ = '0.1'
__license__ = 'WTFPL http://sam.zoy.org/wtfpl/'
### -------------------------------------------------------------------
import os, sys, optparse, subprocess, string
from gdata.youtube.service import YouTubeService
from fnfix import beautify, UGLYCHARS
username = 'MicahElliott'
def get_faves():
"""From a page-limited feed fetches all pages into returned list.
"""
svc = YouTubeService()
feed = svc.GetUserFavoritesFeed(username)
vids = []
while feed:
vids.extend( [vid for vid in feed.entry] )
#print vids[0]
feed = svc.GetNext(feed)
print 'Number of favorites:', len(vids)
return vids
vids = get_faves()
table = string.maketrans('', '')
def fix_title(bad_title):
return beautify(bad_title, table, UGLYCHARS)
#urls_titles = [ (vid.link[0].href, vid.title.text)
urls_titles = [ (vid.link[0].href, fix_title(vid.title.text))
for vid in vids ]
#for (u, t) in (urls_titles): print t
#sys.exit()
for (u, t) in (urls_titles):
print 'thinking about:', t
if os.path.isfile(t+'.flv'):
print 'Already had:', t+'.flv\n'
else:
clive = ['clive', '-O', '%s.flv'%t, '%s'%u]
print "Using 'clive' to pull '%s' from youtube with:" % t
print ' '.join(clive)
try:
code = subprocess.call(clive)
pass
except OSError, e:
print e
#coder = 'ffmpeg -i "%s.flv" -f mp3 -vn -acodec copy "%s.mp3"' % (t, t)
coder = [
'ffmpeg', '-y', '-i', "%s.flv"%t, '-f', 'mp3',
'-vn', '-acodec', 'copy', "%s.mp3"%t ]
print "Converting '%s.mp3' from FLV to MP3 with:" % t
print ' '.join(coder)
subprocess.call(coder)
#sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment