#!/usr/bin/env python import threading import os import sys import time import string from random import choice import datetime class Stages: BASE = "/home/anyclip/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, os.path.splitext(movie)[0], Stages.VIDEOTS, (os.path.splitext(movie)[0] + MovieType.GLUEDVOB)) def mp4(stage, movie): return os.path.join(stage, (os.path.splitext(movie)[0] + MovieType.MP4)) def audioTrack(stage, movie): return os.path.join(stage, os.path.splitext(movie)[0], 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.\"") 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") 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") class Encode(threading.Thread): def run(self): while True: 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("/home/anyclip/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() #if now.hour > 9 and now.hour < 18: # continue 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) 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()