summaryrefslogtreecommitdiffstats
path: root/rip.py
blob: 514356a4ce774b03512295460b8c29dac879e6a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/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()