Ticket #10042: mythburn.py.patch
File mythburn.py.patch, 12.7 KB (added by , 13 years ago) |
---|
-
mythburn.py
old new 38 38 #****************************************************************************** 39 39 40 40 # version of script - change after each update 41 VERSION="0.1.201 01206-1"41 VERSION="0.1.20110821-1" 42 42 43 43 # keep all temporary files for debugging purposes 44 44 # set this to True before a first run through when testing … … 75 75 from fcntl import ioctl 76 76 import CDROM 77 77 from shutil import copy 78 from subprocess import Popen, PIPE 78 79 79 80 # media types (should match the enum in mytharchivewizard.h) 80 81 DVD_SL = 0 … … 606 607 # of a video file from its stream info file 607 608 608 609 def getVideoParams(folder): 609 """Returns the video resolution, fps and aspect ratio for the video file from the streamin do.xml file"""610 """Returns the video resolution, fps and aspect ratio for the video file from the streaminfo.xml file""" 610 611 611 612 #open the XML containing information about this file 612 613 infoDOM = xml.dom.minidom.parse(os.path.join(folder, 'streaminfo.xml')) … … 642 643 # Gets the aspect ratio of a video file from its stream info file 643 644 644 645 def getAspectRatioOfVideo(index): 645 """Returns the aspect ratio of the video file (1.333, 1.778, etc)"""646 """Returns the aspect ratio of the original video file (1.333, 1.778, etc)""" 646 647 647 648 #open the XML containing information about this file 648 infoDOM = xml.dom.minidom.parse(os.path.join(getItemTempPath(index), 'streaminfo .xml'))649 infoDOM = xml.dom.minidom.parse(os.path.join(getItemTempPath(index), 'streaminfo_orig.xml')) 649 650 650 651 #error out if its the wrong XML 651 652 if infoDOM.documentElement.tagName != "file": 652 fatalError("Stream info file doesn't look right (%s)" % os.path.join(getItemTempPath(index), 'streaminfo .xml'))653 fatalError("Stream info file doesn't look right (%s)" % os.path.join(getItemTempPath(index), 'streaminfo_orig.xml')) 653 654 video = infoDOM.getElementsByTagName("file")[0].getElementsByTagName("streams")[0].getElementsByTagName("video")[0] 654 655 if video.attributes["aspectratio"].value != 'N/A': 655 656 aspect_ratio = float(video.attributes["aspectratio"].value) … … 1762 1763 1763 1764 1764 1765 ############################################################# 1766 # Finds the path of a video file from the local video path 1767 # or Storage Group 1768 1769 def getVideoPath(filename): 1770 # connect 1771 db = getDatabaseConnection() 1772 # create a cursor 1773 cursor = db.cursor() 1774 # execute SQL statement 1775 cursor.execute("""SELECT dirname 1776 FROM storagegroup 1777 WHERE groupname='Videos'""") 1778 # get the resultset as a tuple 1779 result = cursor.fetchall() 1780 # make result a list and add local video path if exists 1781 result = [videopath] + list(result) 1782 1783 # iterate through result set 1784 for sg in result: 1785 if doesFileExist(os.path.join("".join(sg), filename)) == True: 1786 filepath = "".join(sg) 1787 write("Video Path: %s" % filepath) 1788 return (filepath) 1789 break 1790 1791 db.close() 1792 del db 1793 del cursor 1794 1795 1796 ############################################################# 1765 1797 # Pre-process a single video/recording file 1766 1798 1767 1799 def preProcessFile(file, folder, count): … … 1775 1807 #3. Extract a single frame from the video to use as a thumbnail and resolution check 1776 1808 mediafile="" 1777 1809 1778 if file.attributes["type"].value =="recording":1810 if file.attributes["type"].value=="recording": 1779 1811 mediafile = file.attributes["filename"].value 1780 elif file.attributes["type"].value =="video":1781 mediafile = os.path.join( videopath, file.attributes["filename"].value)1782 elif file.attributes["type"].value =="file":1812 elif file.attributes["type"].value=="video": 1813 mediafile = os.path.join(getVideoPath(file.attributes["filename"].value), file.attributes["filename"].value) 1814 elif file.attributes["type"].value=="file": 1783 1815 mediafile = file.attributes["filename"].value 1784 1816 else: 1785 1817 fatalError("Unknown type of video file it must be 'recording', 'video' or 'file'.") … … 1935 1967 1936 1968 if result <> 0: 1937 1969 fatalError("Failed while running mytharchivehelper to get stream information from %s" % filename) 1970 1971 #open the XML containing information about this file 1972 infoDOM = xml.dom.minidom.parse(xmlFilename) 1973 1974 #error out if its the wrong XML 1975 if infoDOM.documentElement.tagName != "file": 1976 fatalError("This info file doesn't look right (%s)." % xmlFilename) 1977 1978 file = infoDOM.getElementsByTagName("file")[0] 1979 video = infoDOM.getElementsByTagName("file")[0].getElementsByTagName("streams")[0].getElementsByTagName("video")[0] 1980 1981 #use ffmpeg to get display aspect ratio (DAR) of video 1982 cmd = path_ffmpeg[0] + " -i " + quoteFilename(file.attributes["filename"].value) + " 2>&1" 1983 aspect_ratio = Popen(cmd, shell=True, stdout=PIPE).stdout.read() 1984 if "DAR" in aspect_ratio: 1985 #clean DAR string 1986 aspect_ratio = aspect_ratio.split("DAR ")[-1].split(",")[0] 1987 aspect_ratio = ''.join([c for c in aspect_ratio if c in '1234567890:']).split(":") 1988 else: 1989 #calculate aspect from video size 1990 aspect_ratio = getVideoSize(xmlFilename) 1991 1992 #convert to decimal 1993 aspect_ratio = float(aspect_ratio[0]) / float(aspect_ratio[1]) 1994 1995 write("Video %s aspect ratio is: %s" % (filename, aspect_ratio)) 1996 1997 #set aspect ratio 1998 video.setAttribute("aspectratio",str(aspect_ratio)) 1999 2000 WriteXMLToFile (infoDOM,xmlFilename) 1938 2001 1939 2002 # print out the streaminfo.xml file to the log 1940 infoDOM = xml.dom.minidom.parse(xmlFilename)1941 2003 write("streaminfo.xml :-\n" + infoDOM.toprettyxml(" ", ""), False) 1942 2004 1943 2005 ############################################################# … … 2322 2384 ############################################################# 2323 2385 # Re-encodes a file to mpeg2 2324 2386 2325 def encodeVideoToMPEG2(source, destvideofile, video, audio1, audio2, aspectratio, profile):2387 def encodeVideoToMPEG2(source, destvideofile, video, folder, audio1, audio2, aspectratio, profile): 2326 2388 """Encodes an unknown video source file eg. AVI to MPEG2 video and AC3 audio, use ffmpeg""" 2327 2389 2328 2390 profileNode = findEncodingProfile(profile) … … 2347 2409 value = quoteFilename(destvideofile) 2348 2410 if value == "%aspect": 2349 2411 value = aspectratio 2412 if value == "720x480" or value == "720x576": 2413 #add padding to correct for aspects > than 1.9:1 2414 videores, fps, videoAR = getVideoParams(folder) 2415 if float(videoAR) >= 1.9: 2416 if videomode == "ntsc": 2417 videoheight = 480 2418 else: 2419 videoheight = 576 2420 2421 croppixels = videoheight - int(videores.split("x")[1]) 2422 write("CropPixels Total: %s" % croppixels) 2423 value = "720x%d -vf pad=720:%d:0:%d:black" % (videoheight - croppixels, videoheight, croppixels / 2) 2350 2424 2351 2425 # only re-encode the audio if it is not already in AC3 format 2352 2426 if audio1[AUDIO_CODEC] == "AC3": … … 2383 2457 command += " -newaudio" 2384 2458 2385 2459 #make sure we get the correct stream(s) that we want 2386 command += " -map 0:%d -map 0:%d 2460 command += " -map 0:%d -map 0:%d" % (video[VIDEO_INDEX], audio1[AUDIO_INDEX]) 2387 2461 if audio2[AUDIO_ID] != -1: 2388 2462 command += "-map 0:%d" % (audio2[AUDIO_INDEX]) 2389 2463 2390 2464 if passes == 1: 2391 write( command)2465 write("Running ffmpeg: %s" % command) 2392 2466 result = runCommand(command) 2393 2467 if result!=0: 2394 2468 fatalError("Failed while running ffmpeg to re-encode video.\n" … … 2399 2473 2400 2474 pass1 = string.replace(command, "%passno","1") 2401 2475 pass1 = string.replace(pass1, "%passlogfile", passLog) 2402 write(" Pass 1 - " +pass1)2476 write("Running ffmpeg Pass 1: %s" % pass1) 2403 2477 result = runCommand(pass1) 2404 2478 2405 2479 if result!=0: … … 2411 2485 2412 2486 pass2 = string.replace(command, "%passno","2") 2413 2487 pass2 = string.replace(pass2, "%passlogfile", passLog) 2414 write(" Pass 2 - " +pass2)2488 write("Running ffmpeg Pass 2: %s" % pass2) 2415 2489 result = runCommand(pass2) 2416 2490 2417 2491 if result!=0: … … 2443 2517 outaudiosamplerate = 48000 2444 2518 outaudiocodec = "ac3" 2445 2519 deinterlace = 0 2446 croptop = 02447 cropright = 02448 cropbottom = 02449 cropleft = 02450 2520 qmin = 5 2451 2521 qmax = 31 2452 2522 qdiff = 31 … … 2470 2540 outvideores = value 2471 2541 if name == "-deinterlace": 2472 2542 deinterlace = 1 2473 if name == "-croptop":2474 croptop = value2475 if name == "-cropright":2476 cropright = value2477 if name == "-cropbottom":2478 cropbottom = value2479 if name == "-cropleft":2480 cropleft = value2481 2543 if name == "-qmin": 2482 2544 qmin = value 2483 2545 if name == "-qmax": … … 2526 2588 command += "-aspect %s -r %s " % (aspectratio, fps) 2527 2589 if (deinterlace == 1): 2528 2590 command += "-deinterlace " 2529 command += "-croptop %s -cropright %s -cropbottom %s -cropleft %s " % (croptop, cropright, cropbottom, cropleft)2530 2591 command += "-s %s -b %s -vcodec mpeg2video " % (outvideores, outvideobitrate) 2531 2592 command += "-qmin %s -qmax %s -qdiff %s " % (qmin, qmax, qdiff) 2532 2593 command += "-ab %s -ar %s -acodec %s " % (outaudiobitrate, outaudiosamplerate, outaudiocodec) … … 2543 2604 if (not(doesFileExist(os.path.join(folder, "audout")) and doesFileExist(os.path.join(folder, "vidout")))): 2544 2605 fatalError("Waited too long for mythtranscode to create the fifos - giving up!!") 2545 2606 2546 write("Running ffmpeg ")2607 write("Running ffmpeg: %s" % command) 2547 2608 result = runCommand(command) 2548 2609 if result != 0: 2549 2610 os.kill(PID, signal.SIGKILL) … … 4724 4785 elif file.attributes["type"].value=="recording": 4725 4786 mediafile = file.attributes["filename"].value 4726 4787 elif file.attributes["type"].value=="video": 4727 mediafile =os.path.join(videopath, file.attributes["filename"].value)4788 mediafile = os.path.join(getVideoPath(file.attributes["filename"].value), file.attributes["filename"].value) 4728 4789 elif file.attributes["type"].value=="file": 4729 4790 mediafile=file.attributes["filename"].value 4730 4791 else: … … 4834 4895 starttime = -1 4835 4896 usecutlist = -1 4836 4897 4837 encodeNuvToMPEG2(chanid, starttime, mediafile, os.path.join(folder, "newfile2.mpg"), folder, 4838 profile, usecutlist) 4898 encodeNuvToMPEG2(chanid, starttime, mediafile, os.path.join(folder, "newfile2.mpg"), folder, profile, usecutlist) 4839 4899 mediafile = os.path.join(folder, 'newfile2.mpg') 4840 4900 else: 4841 4901 #we need to re-encode the file, make sure we get the right video/audio streams … … 4861 4921 profile = defaultEncodingProfile 4862 4922 4863 4923 #do the re-encode 4864 encodeVideoToMPEG2(mediafile, os.path.join(folder, "newfile2.mpg"), video, 4865 audio1, audio2, aspectratio, profile) 4924 encodeVideoToMPEG2(mediafile, os.path.join(folder, "newfile2.mpg"), video, folder, audio1, audio2, aspectratio, profile) 4866 4925 mediafile = os.path.join(folder, 'newfile2.mpg') 4867 4926 4868 4927 #remove the old mediafile that was run through mythtranscode … … 4936 4995 elif file.attributes["type"].value=="recording": 4937 4996 mediafile = file.attributes["filename"].value 4938 4997 elif file.attributes["type"].value=="video": 4939 mediafile =os.path.join(videopath, file.attributes["filename"].value)4998 mediafile = os.path.join(getVideoPath(file.attributes["filename"].value), file.attributes["filename"].value) 4940 4999 elif file.attributes["type"].value=="file": 4941 5000 mediafile=file.attributes["filename"].value 4942 5001 else: … … 4988 5047 starttime = -1 4989 5048 usecutlist = -1 4990 5049 4991 encodeNuvToMPEG2(chanid, starttime, mediafile, os.path.join(folder, "newfile2.mpg"), folder, 4992 profile, usecutlist) 5050 encodeNuvToMPEG2(chanid, starttime, mediafile, os.path.join(folder, "newfile2.mpg"), folder, profile, usecutlist) 4993 5051 mediafile = os.path.join(folder, 'newfile2.mpg') 4994 5052 else: 4995 5053 #we need to re-encode the file, make sure we get the right video/audio streams … … 5015 5073 profile = defaultEncodingProfile 5016 5074 5017 5075 #do the re-encode 5018 encodeVideoToMPEG2(mediafile, os.path.join(folder, "newfile2.mpg"), video, 5019 audio1, audio2, aspectratio, profile) 5076 encodeVideoToMPEG2(mediafile, os.path.join(folder, "newfile2.mpg"), video, folder, audio1, audio2, aspectratio, profile) 5020 5077 mediafile = os.path.join(folder, 'newfile2.mpg') 5021 5078 5022 5079 #remove an intermediate file