Synopsis:
mp3hunter is under development to be able to download and encode media from the internet with particular interest in downloading youtube videos and ripping out the audio to mp3 format. However, the program is expanding to serve as a more general purpose internet media tool.
Downloads:
MP3Hunter.py.tar.bz2 [last update: nov_18_2010]
Currently mp3hunter works in grabbing youtube videos and by default will make an mp3 sound only version of the video as well as keep the original, in tact video. You can adjust such action as seen below. The program will always grab the best available format for your video of interest and encodes it to a decent mp3.
Requirements:
To help you build your link list, you can use the bookmarklet outlined below to build a youtube favorite list or playlist, click the youtube_list bookmarklet and it will generate a dialog in your browser with all the video links in that list for you to copy and paste into a text file (see image below of it in action).
The Code:
Usage:
Using a file list (newline delimited)
mp3hunter -f /path/to/mylist.txt
Using a links
mp3hunter -l “http://youtube.com/somevideo1,http://youtube.com/somevideo2″
Keep the video files
mp3hunter -k -f /path/to/mylist.txt
** for now this is default and (temporarily) can’t be changed
Only grab video and don’t make mp3hunter
mp3hunter -n -f /path/to/mylist.txt
#!/usr/bin/env python
#
# mp3hunter by xiao_haozi mutaku.com 2010
# nov 18 2010
# special thanks to youtube-dl authors for such a great tool
# please hack this to pieces and share with friends!
#
import getopt, sys, os, subprocess
def usage(error=""):
howto = '''
Using a file list (newline delimited)
mp3hunter -f /path/to/mylist.txt
Using a links
mp3hunter -l "http://youtube.com/somevideo1,http://youtube.com/somevideo2"
Keep the video files
mp3hunter -k -f /path/to/mylist.txt
** for now this is default and (temporarily) can't be changed
Only grab video and don't make mp3hunter
mp3hunter -n -f /path/to/mylist.txt
'''
print error,howto
sys.exit(2)
def bountyhunter(toget,keepflv=True):
thisvid = toget
getcommand = ["youtube-dl","-w",thisvid]
namecommand = ["youtube-dl","-es",thisvid]
makecommand = ""
thisout = ""
thisdone = False
if "youtube.com/v/" in thisvid:
thisout = thisvid.split('/')[-1].rstrip('\n')
elif "watch?v=" in thisvid:
thisout = thisvid.split('v=')[1].split('&')[0].rstrip('\n')
else:
pass
if not thisout:
print thisvid," is malformed. Skipping."
pass
else:
try:
pt = subprocess.Popen(namecommand, stdout=subprocess.PIPE)
prenamedout = pt.communicate()[0]
baseout = prenamedout.replace(" ","_").replace(".","").rstrip('\n')
namedout = baseout+".mp3"
print
print "Now working on ", namedout
subprocess.call(getcommand)
extsearch = str(thisout+"*")
extcommand = ['find','.','-name',extsearch,'-print']
getext = subprocess.Popen(extcommand,stdout=subprocess.PIPE)
extresult = getext.communicate()[0]
ext = '.'+extresult.rstrip('\n').split('.')[-1]
keepout = baseout+ext
toencode = thisout+ext
makecommand = ["ffmpeg","-i",toencode,"-ab","128000","-ar","44100",namedout]
except:
print thisvid," couldn't be downloaded. Skipping."
if makecommand:
try:
subprocess.call(makecommand)
print "Successfully made ",namedout
thisdone = True
except:
print thisvid," couldn't be converted. Skipping."
if thisdone and not keepflv:
try:
print keepflv
removefile = toencode
os.remove(removefile)
print removefile, " deleted."
except:
print removefile," could't be deleted."
elif thisdone and keepflv:
try:
os.rename(toencode,keepout)
print keepout," kept."
except:
print toencode," could not be renamed."
try:
opts, args = getopt.getopt(sys.argv[1:], "f:khl:")
except getopt.GetoptError, e:
error = str(e)
usage(error)
for o, a in opts:
theinputfile = ""
thelinks = ""
#global keepflv
#keepflv = False
if o in ("-h"):
usage()
elif o in ("-k"):
pass
# keepflv = True
# print "Keeping FLV files."
elif o in ("-f"):
theinputfile = str(a)
elif o in ("-l"):
thelinks = str(a)
else:
error = "Please make sure you have used your options correctly."
usage(error)
if theinputfile:
if os.path.exists(theinputfile):
thefile = os.path.basename(theinputfile)
thepath = os.path.dirname(theinputfile)
else:
error = "This input file does not exist."
usage(error)
f = open(theinputfile,'r')
for line in f:
bountyhunter(line)
print
f.close()
if thelinks:
for link in thelinks.split(','):
if len(link) > 5:
bountyhunter(link)
print
Installing the bookmarklet… simply copy the code below and create a new bookmark in your browser’s bookmark toolbar. Name it something like youtube_list and then for the link url simply paste in the JS code below. That’s it. Simply navigate to your desired youtube list, click the bookmarklet, copy and paste the links into your text file and run mp3hunter on it.
javascript:
if (typeof jQuery == 'undefined') {
var jQ = document.createElement('script');
jQ.type = 'text/javascript';
jQ.onload=runthis;
jQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
document.body.appendChild(jQ);
}
else {
runthis();
}
function runthis() {
var thelinks = "";
var thislink;
$('div.vm-video-title').each(function() {
thislink = $(this).find('a').attr('href');
thislink = "http://youtube.com" + thislink + "\n";
thelinks = thelinks + thislink;
});
if(thelinks.length != 0){
alert(thelinks);
}
};
Summary:
There are lots of options out there for doing what mp3hunter tries to do, and probably much better at doing so. However, this was mainly just a quick wrapper for youtube-dl and served it’s purpose well enough.
- Matthew
