summaryrefslogtreecommitdiffstats
path: root/abs/core/mythtv/stable-0.24/mythplugins/mythburn.py.patch
blob: abac7dfe669d43558a8ffa263de67004bc20e507 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
--- mythburn.py.orig	2011-09-16 13:41:43.000000000 -0500
+++ mythburn.py	2011-09-17 10:51:36.000000000 -0500
@@ -38,7 +38,7 @@
 #******************************************************************************
 
 # version of script - change after each update
-VERSION="0.1.20101206-1"
+VERSION="0.1.20110821-1"
 
 # keep all temporary files for debugging purposes
 # set this to True before a first run through when testing
@@ -75,6 +75,7 @@
 from fcntl import ioctl
 import CDROM
 from shutil import copy
+from subprocess import Popen, PIPE
 
 # media types (should match the enum in mytharchivewizard.h)
 DVD_SL = 0
@@ -606,7 +607,7 @@
 # of a video file from its stream info file
 
 def getVideoParams(folder):
-    """Returns the video resolution, fps and aspect ratio for the video file from the streamindo.xml file"""
+    """Returns the video resolution, fps and aspect ratio for the video file from the streaminfo.xml file"""
 
     #open the XML containing information about this file
     infoDOM = xml.dom.minidom.parse(os.path.join(folder, 'streaminfo.xml'))
@@ -642,14 +643,14 @@
 # Gets the aspect ratio of a video file from its stream info file
 
 def getAspectRatioOfVideo(index):
-    """Returns the aspect ratio of the video file (1.333, 1.778, etc)"""
+    """Returns the aspect ratio of the original video file (1.333, 1.778, etc)"""
 
     #open the XML containing information about this file
-    infoDOM = xml.dom.minidom.parse(os.path.join(getItemTempPath(index), 'streaminfo.xml'))
+    infoDOM = xml.dom.minidom.parse(os.path.join(getItemTempPath(index), 'streaminfo_orig.xml'))
 
     #error out if its the wrong XML
     if infoDOM.documentElement.tagName != "file":
-        fatalError("Stream info file doesn't look right (%s)" % os.path.join(getItemTempPath(index), 'streaminfo.xml'))
+        fatalError("Stream info file doesn't look right (%s)" % os.path.join(getItemTempPath(index), 'streaminfo_orig.xml'))
     video = infoDOM.getElementsByTagName("file")[0].getElementsByTagName("streams")[0].getElementsByTagName("video")[0]
     if video.attributes["aspectratio"].value != 'N/A':
         aspect_ratio = float(video.attributes["aspectratio"].value)
@@ -1762,6 +1763,37 @@
 
 
 #############################################################
+# Finds the path of a video file from the local video path
+# or Storage Group
+
+def getVideoPath(filename):
+     # connect
+    db = getDatabaseConnection()
+    # create a cursor
+    cursor = db.cursor()
+    # execute SQL statement
+    cursor.execute("""SELECT dirname 
+                      FROM storagegroup 
+                      WHERE groupname='Videos'""")
+    # get the resultset as a tuple
+    result = cursor.fetchall()
+    # make result a list and add local video path if exists
+    result = [videopath] + list(result)
+    
+    # iterate through result set
+    for sg in result:
+        if doesFileExist(os.path.join("".join(sg), filename)) == True:
+            filepath = "".join(sg)
+            write("Video Path: %s" % filepath)
+            return (filepath)
+            break
+
+    db.close()
+    del db
+    del cursor
+
+
+#############################################################
 # Pre-process a single video/recording file
 
 def preProcessFile(file, folder, count):
@@ -1775,11 +1807,11 @@
     #3. Extract a single frame from the video to use as a thumbnail and resolution check
     mediafile=""
 
-    if file.attributes["type"].value == "recording":
+    if file.attributes["type"].value=="recording":
         mediafile = file.attributes["filename"].value
-    elif file.attributes["type"].value == "video":
-        mediafile = os.path.join(videopath, file.attributes["filename"].value)
-    elif file.attributes["type"].value == "file":
+    elif file.attributes["type"].value=="video":
+        mediafile = os.path.join(getVideoPath(file.attributes["filename"].value), file.attributes["filename"].value)
+    elif file.attributes["type"].value=="file":
         mediafile = file.attributes["filename"].value
     else:
         fatalError("Unknown type of video file it must be 'recording', 'video' or 'file'.")
@@ -1935,9 +1967,39 @@
 
     if result <> 0:
         fatalError("Failed while running mytharchivehelper to get stream information from %s" % filename)
+    
+    #open the XML containing information about this file
+    infoDOM = xml.dom.minidom.parse(xmlFilename)
+    
+    #error out if its the wrong XML
+    if infoDOM.documentElement.tagName != "file":
+        fatalError("This info file doesn't look right (%s)." % xmlFilename)
+    
+    file = infoDOM.getElementsByTagName("file")[0]
+    video = infoDOM.getElementsByTagName("file")[0].getElementsByTagName("streams")[0].getElementsByTagName("video")[0]
+    
+    #use ffmpeg to get display aspect ratio (DAR) of video
+    cmd = path_ffmpeg[0] + " -i " + quoteFilename(file.attributes["filename"].value) + " 2>&1"
+    aspect_ratio = Popen(cmd, shell=True, stdout=PIPE).stdout.read()
+    if "DAR" in aspect_ratio:
+        #clean DAR string
+        aspect_ratio = aspect_ratio.split("DAR ")[-1].split(",")[0]
+        aspect_ratio = ''.join([c for c in aspect_ratio if c in '1234567890:']).split(":")
+    else:
+        #calculate aspect from video size
+        aspect_ratio = getVideoSize(xmlFilename)
+
+    #convert to decimal
+    aspect_ratio = float(aspect_ratio[0]) / float(aspect_ratio[1])
+
+    write("Video %s aspect ratio is: %s" % (filename, aspect_ratio))
+
+    #set aspect ratio
+    video.setAttribute("aspectratio",str(aspect_ratio))
+    
+    WriteXMLToFile (infoDOM,xmlFilename)
 
     # print out the streaminfo.xml file to the log
-    infoDOM = xml.dom.minidom.parse(xmlFilename)
     write("streaminfo.xml :-\n" + infoDOM.toprettyxml("    ", ""), False)
 
 #############################################################
@@ -2322,7 +2384,7 @@
 #############################################################
 # Re-encodes a file to mpeg2
 
-def encodeVideoToMPEG2(source, destvideofile, video, audio1, audio2, aspectratio, profile):
+def encodeVideoToMPEG2(source, destvideofile, video, folder, audio1, audio2, aspectratio, profile):
     """Encodes an unknown video source file eg. AVI to MPEG2 video and AC3 audio, use ffmpeg"""
 
     profileNode = findEncodingProfile(profile)
@@ -2347,6 +2409,18 @@
             value = quoteFilename(destvideofile)
         if value == "%aspect":
             value = aspectratio
+        if value == "720x480" or value == "720x576":
+            #add padding to correct for aspects > than 1.9:1
+            videores, fps, videoAR = getVideoParams(folder)
+            if float(videoAR) >= 1.9:
+                if videomode == "ntsc":
+                    videoheight = 480
+                else:
+                    videoheight = 576
+        
+                croppixels = videoheight - (720 / float(videoAR))
+                write("CropPixels Total: %s" % croppixels)
+                value = "720x%d -vf pad=720:%d:0:%d:black" % (videoheight - croppixels, videoheight, croppixels / 2)
 
         # only re-encode the audio if it is not already in AC3 format
         if audio1[AUDIO_CODEC] == "AC3":
@@ -2383,12 +2457,12 @@
         command += " -newaudio" 
 
     #make sure we get the correct stream(s) that we want
-    command += " -map 0:%d -map 0:%d " % (video[VIDEO_INDEX], audio1[AUDIO_INDEX])
+    command += " -map 0:%d -map 0:%d" % (video[VIDEO_INDEX], audio1[AUDIO_INDEX])
     if audio2[AUDIO_ID] != -1:
         command += "-map 0:%d" % (audio2[AUDIO_INDEX])
 
     if passes == 1:
-        write(command)
+        write("Running ffmpeg: %s" % command)
         result = runCommand(command)
         if result!=0:
             fatalError("Failed while running ffmpeg to re-encode video.\n"
@@ -2399,7 +2473,7 @@
 
         pass1 = string.replace(command, "%passno","1")
         pass1 = string.replace(pass1, "%passlogfile", passLog)
-        write("Pass 1 - " + pass1)
+        write("Running ffmpeg Pass 1: %s" % pass1)
         result = runCommand(pass1)
 
         if result!=0:
@@ -2411,7 +2485,7 @@
 
         pass2 = string.replace(command, "%passno","2")
         pass2 = string.replace(pass2, "%passlogfile", passLog)
-        write("Pass 2 - " + pass2)
+        write("Running ffmpeg Pass 2: %s" % pass2)
         result = runCommand(pass2)
 
         if result!=0:
@@ -2443,10 +2517,6 @@
     outaudiosamplerate = 48000
     outaudiocodec = "ac3"
     deinterlace = 0
-    croptop = 0
-    cropright = 0
-    cropbottom = 0
-    cropleft = 0
     qmin = 5
     qmax = 31
     qdiff = 31
@@ -2470,14 +2540,6 @@
             outvideores = value
         if name == "-deinterlace":
             deinterlace = 1
-        if name == "-croptop":
-            croptop = value
-        if name == "-cropright":
-            cropright = value
-        if name == "-cropbottom":
-            cropbottom = value
-        if name == "-cropleft":
-           cropleft = value
         if name == "-qmin":
            qmin = value
         if name == "-qmax":
@@ -2526,7 +2588,6 @@
     command += "-aspect %s -r %s " % (aspectratio, fps)
     if (deinterlace == 1):
         command += "-deinterlace "
-    command += "-croptop %s -cropright %s -cropbottom %s -cropleft %s " % (croptop, cropright, cropbottom, cropleft)
     command += "-s %s -b %s -vcodec mpeg2video " % (outvideores, outvideobitrate)
     command += "-qmin %s -qmax %s -qdiff %s " % (qmin, qmax, qdiff)
     command += "-ab %s -ar %s -acodec %s " % (outaudiobitrate, outaudiosamplerate, outaudiocodec)
@@ -2543,7 +2604,7 @@
     if (not(doesFileExist(os.path.join(folder, "audout")) and doesFileExist(os.path.join(folder, "vidout")))):
         fatalError("Waited too long for mythtranscode to create the fifos - giving up!!")
 
-    write("Running ffmpeg")
+    write("Running ffmpeg: %s" % command)
     result = runCommand(command)
     if result != 0:
         os.kill(PID, signal.SIGKILL)
@@ -4724,7 +4785,7 @@
     elif file.attributes["type"].value=="recording":
         mediafile = file.attributes["filename"].value
     elif file.attributes["type"].value=="video":
-        mediafile=os.path.join(videopath, file.attributes["filename"].value)
+        mediafile = os.path.join(getVideoPath(file.attributes["filename"].value), file.attributes["filename"].value)
     elif file.attributes["type"].value=="file":
         mediafile=file.attributes["filename"].value
     else:
@@ -4834,8 +4895,7 @@
                 starttime = -1
                 usecutlist = -1
 
-            encodeNuvToMPEG2(chanid, starttime, mediafile, os.path.join(folder, "newfile2.mpg"), folder,
-                         profile, usecutlist)
+            encodeNuvToMPEG2(chanid, starttime, mediafile, os.path.join(folder, "newfile2.mpg"), folder, profile, usecutlist)
             mediafile = os.path.join(folder, 'newfile2.mpg')
         else:
             #we need to re-encode the file, make sure we get the right video/audio streams
@@ -4861,8 +4921,7 @@
                 profile = defaultEncodingProfile
 
             #do the re-encode 
-            encodeVideoToMPEG2(mediafile, os.path.join(folder, "newfile2.mpg"), video,
-                            audio1, audio2, aspectratio, profile)
+            encodeVideoToMPEG2(mediafile, os.path.join(folder, "newfile2.mpg"), video, folder, audio1, audio2, aspectratio, profile)
             mediafile = os.path.join(folder, 'newfile2.mpg')
 
             #remove the old mediafile that was run through mythtranscode
@@ -4936,7 +4995,7 @@
     elif file.attributes["type"].value=="recording":
         mediafile = file.attributes["filename"].value
     elif file.attributes["type"].value=="video":
-        mediafile=os.path.join(videopath, file.attributes["filename"].value)
+        mediafile = os.path.join(getVideoPath(file.attributes["filename"].value), file.attributes["filename"].value)
     elif file.attributes["type"].value=="file":
         mediafile=file.attributes["filename"].value
     else:
@@ -4988,8 +5047,7 @@
                 starttime = -1
                 usecutlist = -1
 
-            encodeNuvToMPEG2(chanid, starttime, mediafile, os.path.join(folder, "newfile2.mpg"), folder,
-                         profile, usecutlist)
+            encodeNuvToMPEG2(chanid, starttime, mediafile, os.path.join(folder, "newfile2.mpg"), folder, profile, usecutlist)
             mediafile = os.path.join(folder, 'newfile2.mpg')
         else:
             #we need to re-encode the file, make sure we get the right video/audio streams
@@ -5015,8 +5073,7 @@
                 profile = defaultEncodingProfile
 
             #do the re-encode 
-            encodeVideoToMPEG2(mediafile, os.path.join(folder, "newfile2.mpg"), video,
-                            audio1, audio2, aspectratio, profile)
+            encodeVideoToMPEG2(mediafile, os.path.join(folder, "newfile2.mpg"), video, folder, audio1, audio2, aspectratio, profile)
             mediafile = os.path.join(folder, 'newfile2.mpg')
 
     #remove an intermediate file