summaryrefslogtreecommitdiffstats
path: root/rip.py
blob: bcb355da3df2280401aefb95f53f7249eeccadfd (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
#!/usr/bin/env python

import threading
import os
import sys
import time
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 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")
			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:
						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)
			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)

class Upload(threading.Thread):
	def run(self):
		while True:
			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)

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")

Rip().start()
Encode().start()
Upload().start()