diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2009-07-07 14:39:36 -0400 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2009-07-07 14:39:36 -0400 |
commit | 1bb6566f842175306955ac5021e33d8cfd64a2d6 (patch) | |
tree | 36e68d8afdc6cd9fc832a70adc395e129e227da0 | |
parent | Syntax. (diff) | |
download | OldSchoolRipper-1bb6566f842175306955ac5021e33d8cfd64a2d6.tar.xz OldSchoolRipper-1bb6566f842175306955ac5021e33d8cfd64a2d6.zip |
Major code cleanup with regard to paths.
-rwxr-xr-x | rip.py | 249 |
1 files changed, 155 insertions, 94 deletions
@@ -8,121 +8,182 @@ import string from random import choice import datetime -def makeRandomString(length=8, chars=string.ascii_letters + string.digits): - return ''.join([choice(chars) for i in range(length)]) +class Stages: + BASE = "/Users/zx2c4/Desktop" + RIPPING = os.path.join(BASE, "ripping") + ENCODING = os.path.join(BASE, "encoding") + ENCODED = os.path.join(BASE, "encoded") + TOENCODE = os.path.join(BASE, "toencode") + UPLOADED = os.path.join(BASE, "uploaded") + TOUPLOAD = os.path.join(BASE, "toupload") + VIDEOTS = "VIDEO_TS" + @staticmethod + def createStage(stage): + if not os.path.exists(stage): + os.makedirs(stage) + +class MovieType: + RAWVOB = ".VOB" + GLUEDVOB = ".vob" + MP4 = ".mp4" + AUDIOTRACK = "audiotrack.txt" + @staticmethod + def getType(entry): + return os.path.splitext(entry)[1] + +def rawVobs(stage, movie): + vobDir = os.path.join(stage, movie, Stages.VIDEOTS) + if os.path.exists(vobDir): + vobs = os.listdir(vobDir) + def isVob(entry): + return os.path.isfile(entry) and MovieType.getType(entry) == MovieType.RAWVOB + def prependStage(entry): + return os.path.join(vobDir, entry) + vobs = filter(isVob, map(prependStage, vobs)) + vobs.sort() + return vobs + return [] + +def gluedVob(stage, movie): + return os.path.join(stage, movie, Stages.VIDEOTS, (movie + MovieType.GLUEDVOB)) + +def mp4(stage, movie): + return os.path.join(stage, (movie + MovieType.MP4)) + +def audioTrack(stage, movie): + return os.path.join(stage, movie, Stages.VIDEOTS, MovieType.AUDIOTRACK) + +def moviesInStage(stage): + if os.path.exists(stage): + movies = os.listdir(stage) + def isMovieDir(entry): + joined = os.path.join(stage, entry) + return os.path.isdir(joined) or MovieType.getType(joined) == MovieType.MP4 + movies = filter(isMovieDir, movies) + movies.sort() + return movies + return [] + +def moveStages(origin, destination, movie): + origin = os.path.join(origin, movie) + destination = os.path.join(destination, movie) + if os.path.exists(destination): + return False + if os.path.exists(origin): + os.rename(origin, destination) + print "Moving %s to %s" (origin, destination) + return True + return False + +log = open(os.path.join(Stages.BASE, "ripperlog.csv"), "a") +def logEvent(event, target="-"): + log.write(time.asctime() + "," + event + "," + target + "\n") + log.flush() class Rip(threading.Thread): def run(self): while True: os.system("echo \"Please enter next disk.\" | festival --tts ") os.system("zenity --info --text=\"Please enter a DVD, and press OK.\"") - os.system("zenity --entry --text=\"Enter a title, and press OK. Do not enter any spaces; use underscores instead.\" --entry-text=`volname` > dvdname") + dvdtitle = os.tempnam() + os.system("zenity --entry --text=\"Enter a title, and press OK. Do not enter any spaces; use underscores instead.\" --entry-text=`volname` > '%s'" % dvdtitle) logEvent("DVD Rip Begin") - os.system("dvdbackup -v -o./ripping -F --name=`cat dvdname`") - for movie in os.listdir("./ripping"): - try: - movies = os.listdir("./ripping/%s/VIDEO_TS/" % movie) - def isVob(file): return file.endswith(".VOB") - movies = filter(isVob, movies) - movies.sort() - totalSize = 0 - if len(movies) >= 3: - if os.path.getsize("./ripping/%s/VIDEO_TS/%s" % (movie, movies[0])) + 300 < os.path.getsize("./ripping/%s/VIDEO_TS/%s" % (movie, movies[1])): - del movies[0] - for vob in movies: - totalSize += os.path.getsize("./ripping/%s/VIDEO_TS/%s" % (movie, vob)) - partialSize = 0 - twentyPercent = movies[0] - for vob in movies: - partialSize += os.path.getsize("./ripping/%s/VIDEO_TS/%s" % (movie, vob)) - if float(partialSize) / float(totalSize) >= .2: - twentyPercent = vob - break - os.system("vlc ./ripping/%s/VIDEO_TS/%s" % (movie, twentyPercent)) - os.system("zenity --entry --text=\"Please enter the correct audio track number. This must be a digit.\" --entry-text=1 > ./ripping/%s/VIDEO_TS/audiotrack.txt" % movie) - except: - pass - if not os.path.exists("./ripping/%s/VIDEO_TS/audiotrack.txt" % movie): - os.system("echo 1 > ./ripping/%s/VIDEO_TS/audiotrack.txt" % movie) + if os.system("dvdbackup -o '%s' -F --name=`cat '%s'`" % (Stages.RIPPING, dvdtitle)) != 0: + os.remove(dvdtitle) + logEvent("DVD Ripping Failed") + print "Ripping failed" + continue + os.remove(dvdtitle) + for movie in moviesInStage(Stages.RIPPING): + vobs = rawVobs(Stages.RIPPING, movie) + totalSize = 0 + if len(vobs) >= 3: + if os.path.getsize(vobs[0]) + 300 < os.path.getsize(vobs[1]): + print "Removing %s" % vobs[0] + logEvent("Removing Menu", vobs[0]) + os.remove(vobs[0]) + del vobs[0] + for vob in vobs: + totalSize += os.path.getsize(vob) + partialSize = 0 + twentyPercent = vobs[0] + for vob in vobs: + partialSize += os.path.getsize(vob) + if float(partialSize) / float(totalSize) >= .2: + twentyPercent = vob + break + os.system("echo \"Please select correct audiotrack.\" | festival --tts ") + os.system("vlc '%s'" % vob) + os.system("zenity --entry --text=\"Please enter the correct audio track number. This must be a digit.\" --entry-text=1 > '%s'" % audioTrack(Stages.RIPPING, movie)) + if not os.path.exists(audioTrack(Stages.RIPPING, movie)): + audioTrackFile = open(audioTrack(Stages.RIPPING, movie), "w") + audioTrackFile.write("1") + audioTrackFile.close() + if not moveStages(Stages.RIPPING, Stages.TOENCODE, movie): + print "Could not move %s" % movie + logEvent("Could not move from ripping", movie) logEvent("DVD Rip End") os.system("eject /dev/dvd") - os.system("mv ./ripping/* ./toencode/") class Encode(threading.Thread): def run(self): while True: - listing = os.listdir("./toencode") - for i in range(len(listing)): - for j in range(0, 16): - if i < 10: - num = "0" + str(j) - else: - num = str(j) - template = "./toencode/" + listing[i] + "/VIDEO_TS/VTS_" + num + "_%s.VOB" - if os.path.exists(template % "0") and os.path.exists(template % "1") and os.path.exists(template % "2"): - if (os.path.getsize(template % "0") + 300) < os.path.getsize(template % "1"): - os.remove(template % "0") - print "Removing " + (template % "0") - logEvent("Encoding Begin", listing[i]) - if (not os.path.exists("./toencode/" + listing[i] + "/VIDEO_TS/" + listing[i] + ".vob")) or os.path.getsize("./toencode/" + listing[i] + "/VIDEO_TS/" + listing[i] + ".vob") <= 100: - print "Concatinating VOBs" - os.system("cat ./toencode/" + listing[i] + "/VIDEO_TS/*.VOB > ./toencode/" + listing[i] + "/VIDEO_TS/" + listing[i] + ".vob") - if os.path.exists("./toencode/" + listing[i] + "/VIDEO_TS/" + listing[i] + ".vob") and os.path.getsize("./toencode/" + listing[i] + "/VIDEO_TS/" + listing[i] + ".vob") > 100: - os.system("rm ./toencode/" + listing[i] + "/VIDEO_TS/*.VOB") - os.system("/home/anyclip/HandBrakeCLI -i ./toencode/" + listing[i] + "/VIDEO_TS/" + listing[i] + ".vob -o " + listing[i] + ".mp4 -a `cat ./toencode/" + listing[i] + "/VIDEO_TS/audiotrack.txt` -e x264 -b 500 -E faac -B 96 -R Auto -6 stereo --optimize --decomb --deblock --denoise=\"weak\" -f mp4 -P -2 -T -x ref=3:mixed-refs:bframes=6:weightb:direct=auto:b-pyramid:me=umh:subme=9:analyse=all:8x8dct:trellis=1:no-fast-pskip:psy-rd=1,1") - logEvent("Encoding End", listing[i]) - try: - os.rename(listing[i] + ".mp4", "./toupload/" + listing[i] + ".mp4") - os.rename("./toencode/" + listing[i], "./encoded/" + listing[i]) - except: - print "Nothing encoded - could not find file to move." - time.sleep(4) + for movie in moviesInStage(Stages.TOENCODE): + toCat = rawVobs(Stages.TOENCODE, movie) + glued = gluedVob(Stages.TOENCODE, movie) + if len(toCat) > 0: + logEvent("Concatination Begin", movie) + catPaths = "" + for catPath in toCat: + catPaths += "'%s' " % catPath + if os.system("cat %s > '%s'" % (catPaths, glued)) != 0: + print "Concatination failed!!" + logEvent("Concatination Failure", movie) + continue + logEvent("Concatination End", movie) + for toDelete in toCat: + os.remove(toDelete) + logEvent("Encoding Begin", movie) + encoded = mp4(Stages.ENCODING, movie) + os.system("HandBrakeCLI -i '%s' -o '%s' -a `cat %s` -e x264 -b 500 -E faac -B 96 -R Auto -6 stereo --optimize --decomb --deblock --denoise=\"weak\" -f mp4 -P -2 -T -x ref=3:mixed-refs:bframes=6:weightb:direct=auto:b-pyramid:me=umh:subme=9:analyse=all:8x8dct:trellis=1:no-fast-pskip:psy-rd=1,1" % (glued, encoded, audioTrack(Stages.TOENCODE, movie))) + moveStages(Stages.TOENCODE, Stages.ENCODED, movie) + for encodedMovie in moviesInStage(Stages.ENCODING): + moveStages(Stages.ENCODING, Stages.TOUPLOAD, encodedMovie) + logEvent("Encoding End", movie) + time.sleep(10) class Upload(threading.Thread): def run(self): while True: - now = datetime.datetime.now() + #now = datetime.datetime.now() #if now.hour > 9 and now.hour < 18: # continue - listing = os.listdir("./toupload") - for i in range(len(listing)): - print "Uploading " + listing[i] - logEvent("Uploading Begin", listing[i]) - randomString = makeRandomString() - if os.system("wput %s ftp://chris@anyclip.com:ChrisEdge1234@ftp2.edgecastcdn.net/movies/%s_400.mp4" % ("./toupload/" + listing[i], randomString)) == 0: - correlation = open("correlation.csv", "a") - correlation.write(listing[i] + "," + randomString + "," + time.asctime() + "\n") - correlation.close() - logEvent("Uploading End", listing[i]) - try: - os.rename("./toupload/" + listing[i], "./uploaded/" + listing[i]) - except: - print "Upload failed." - logEvent("Uploading Failure", listing[i]) - time.sleep(120) - else: - print "Upload failed." - logEvent("Uploading Failure", listing[i]) - time.sleep(120) - time.sleep(60) + for movie in moviesInStage(Stages.TOUPLOAD): + print "Uploading " + movie + logEvent("Uploading Begin", movie) + randomString = ''.join([choice(string.ascii_letters + string.digits) for i in range(8)]) + if os.system("wput %s ftp://chris@anyclip.com:ChrisEdge1234@ftp2.edgecastcdn.net/movies/%s_400.mp4" % (mp4(Stages.TOUPLOAD, movie), randomString)) == 0: + correlation = open("correlation.csv", "a") + correlation.write(movie + "," + randomString + "," + time.asctime() + "\n") + correlation.close() + logEvent("Uploading End", movie) + moveStages(Stages.TOUPLOAD, Stages.UPLOADED, movie) + else: + print "Upload failed." + logEvent("Uploading Failure", movie) + correlation = open("deleteFromServer.csv", "a") + correlation.write(movie + "," + randomString + "," + time.asctime() + "\n") + correlation.close() + time.sleep(110) + time.sleep(10) -if not os.path.exists("./toupload"): - os.mkdir("./toupload") -if not os.path.exists("./uploaded"): - os.mkdir("./uploaded") -if not os.path.exists("./toencode"): - os.mkdir("./toencode") -if not os.path.exists("./encoded"): - os.mkdir("./encoded") -if not os.path.exists("./ripping"): - os.mkdir("./ripping") - -log = open("ripperlog.csv", "a") -def logEvent(event, target="-"): - log.write(time.asctime() + "," + event + "," + target + "\n") - log.flush() logEvent("Program Start") - +Stages.createStage(Stages.RIPPING) +Stages.createStage(Stages.ENCODING) +Stages.createStage(Stages.ENCODED) +Stages.createStage(Stages.TOENCODE) +Stages.createStage(Stages.UPLOADED) +Stages.createStage(Stages.TOUPLOAD) Rip().start() Encode().start() Upload().start() |