Ticket #10042: mythburn.py.patch

File mythburn.py.patch, 12.7 KB (added by Britney Fransen <britney.fransen@…>, 9 years ago)
  • mythburn.py

    old new  
    3838#******************************************************************************
    3939
    4040# version of script - change after each update
    41 VERSION="0.1.20101206-1"
     41VERSION="0.1.20110821-1"
    4242
    4343# keep all temporary files for debugging purposes
    4444# set this to True before a first run through when testing
     
    7575from fcntl import ioctl
    7676import CDROM
    7777from shutil import copy
     78from subprocess import Popen, PIPE
    7879
    7980# media types (should match the enum in mytharchivewizard.h)
    8081DVD_SL = 0
     
    606607# of a video file from its stream info file
    607608
    608609def getVideoParams(folder):
    609     """Returns the video resolution, fps and aspect ratio for the video file from the streamindo.xml file"""
     610    """Returns the video resolution, fps and aspect ratio for the video file from the streaminfo.xml file"""
    610611
    611612    #open the XML containing information about this file
    612613    infoDOM = xml.dom.minidom.parse(os.path.join(folder, 'streaminfo.xml'))
     
    642643# Gets the aspect ratio of a video file from its stream info file
    643644
    644645def 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)"""
    646647
    647648    #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'))
    649650
    650651    #error out if its the wrong XML
    651652    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'))
    653654    video = infoDOM.getElementsByTagName("file")[0].getElementsByTagName("streams")[0].getElementsByTagName("video")[0]
    654655    if video.attributes["aspectratio"].value != 'N/A':
    655656        aspect_ratio = float(video.attributes["aspectratio"].value)
     
    17621763
    17631764
    17641765#############################################################
     1766# Finds the path of a video file from the local video path
     1767# or Storage Group
     1768
     1769def 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#############################################################
    17651797# Pre-process a single video/recording file
    17661798
    17671799def preProcessFile(file, folder, count):
     
    17751807    #3. Extract a single frame from the video to use as a thumbnail and resolution check
    17761808    mediafile=""
    17771809
    1778     if file.attributes["type"].value == "recording":
     1810    if file.attributes["type"].value=="recording":
    17791811        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":
    17831815        mediafile = file.attributes["filename"].value
    17841816    else:
    17851817        fatalError("Unknown type of video file it must be 'recording', 'video' or 'file'.")
     
    19351967
    19361968    if result <> 0:
    19371969        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)
    19382001
    19392002    # print out the streaminfo.xml file to the log
    1940     infoDOM = xml.dom.minidom.parse(xmlFilename)
    19412003    write("streaminfo.xml :-\n" + infoDOM.toprettyxml("    ", ""), False)
    19422004
    19432005#############################################################
     
    23222384#############################################################
    23232385# Re-encodes a file to mpeg2
    23242386
    2325 def encodeVideoToMPEG2(source, destvideofile, video, audio1, audio2, aspectratio, profile):
     2387def encodeVideoToMPEG2(source, destvideofile, video, folder, audio1, audio2, aspectratio, profile):
    23262388    """Encodes an unknown video source file eg. AVI to MPEG2 video and AC3 audio, use ffmpeg"""
    23272389
    23282390    profileNode = findEncodingProfile(profile)
     
    23472409            value = quoteFilename(destvideofile)
    23482410        if value == "%aspect":
    23492411            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)
    23502424
    23512425        # only re-encode the audio if it is not already in AC3 format
    23522426        if audio1[AUDIO_CODEC] == "AC3":
     
    23832457        command += " -newaudio"
    23842458
    23852459    #make sure we get the correct stream(s) that we want
    2386     command += " -map 0:%d -map 0:%d " % (video[VIDEO_INDEX], audio1[AUDIO_INDEX])
     2460    command += " -map 0:%d -map 0:%d" % (video[VIDEO_INDEX], audio1[AUDIO_INDEX])
    23872461    if audio2[AUDIO_ID] != -1:
    23882462        command += "-map 0:%d" % (audio2[AUDIO_INDEX])
    23892463
    23902464    if passes == 1:
    2391         write(command)
     2465        write("Running ffmpeg: %s" % command)
    23922466        result = runCommand(command)
    23932467        if result!=0:
    23942468            fatalError("Failed while running ffmpeg to re-encode video.\n"
     
    23992473
    24002474        pass1 = string.replace(command, "%passno","1")
    24012475        pass1 = string.replace(pass1, "%passlogfile", passLog)
    2402         write("Pass 1 - " + pass1)
     2476        write("Running ffmpeg Pass 1: %s" % pass1)
    24032477        result = runCommand(pass1)
    24042478
    24052479        if result!=0:
     
    24112485
    24122486        pass2 = string.replace(command, "%passno","2")
    24132487        pass2 = string.replace(pass2, "%passlogfile", passLog)
    2414         write("Pass 2 - " + pass2)
     2488        write("Running ffmpeg Pass 2: %s" % pass2)
    24152489        result = runCommand(pass2)
    24162490
    24172491        if result!=0:
     
    24432517    outaudiosamplerate = 48000
    24442518    outaudiocodec = "ac3"
    24452519    deinterlace = 0
    2446     croptop = 0
    2447     cropright = 0
    2448     cropbottom = 0
    2449     cropleft = 0
    24502520    qmin = 5
    24512521    qmax = 31
    24522522    qdiff = 31
     
    24702540            outvideores = value
    24712541        if name == "-deinterlace":
    24722542            deinterlace = 1
    2473         if name == "-croptop":
    2474             croptop = value
    2475         if name == "-cropright":
    2476             cropright = value
    2477         if name == "-cropbottom":
    2478             cropbottom = value
    2479         if name == "-cropleft":
    2480            cropleft = value
    24812543        if name == "-qmin":
    24822544           qmin = value
    24832545        if name == "-qmax":
     
    25262588    command += "-aspect %s -r %s " % (aspectratio, fps)
    25272589    if (deinterlace == 1):
    25282590        command += "-deinterlace "
    2529     command += "-croptop %s -cropright %s -cropbottom %s -cropleft %s " % (croptop, cropright, cropbottom, cropleft)
    25302591    command += "-s %s -b %s -vcodec mpeg2video " % (outvideores, outvideobitrate)
    25312592    command += "-qmin %s -qmax %s -qdiff %s " % (qmin, qmax, qdiff)
    25322593    command += "-ab %s -ar %s -acodec %s " % (outaudiobitrate, outaudiosamplerate, outaudiocodec)
     
    25432604    if (not(doesFileExist(os.path.join(folder, "audout")) and doesFileExist(os.path.join(folder, "vidout")))):
    25442605        fatalError("Waited too long for mythtranscode to create the fifos - giving up!!")
    25452606
    2546     write("Running ffmpeg")
     2607    write("Running ffmpeg: %s" % command)
    25472608    result = runCommand(command)
    25482609    if result != 0:
    25492610        os.kill(PID, signal.SIGKILL)
     
    47244785    elif file.attributes["type"].value=="recording":
    47254786        mediafile = file.attributes["filename"].value
    47264787    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)
    47284789    elif file.attributes["type"].value=="file":
    47294790        mediafile=file.attributes["filename"].value
    47304791    else:
     
    48344895                starttime = -1
    48354896                usecutlist = -1
    48364897
    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)
    48394899            mediafile = os.path.join(folder, 'newfile2.mpg')
    48404900        else:
    48414901            #we need to re-encode the file, make sure we get the right video/audio streams
     
    48614921                profile = defaultEncodingProfile
    48624922
    48634923            #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)
    48664925            mediafile = os.path.join(folder, 'newfile2.mpg')
    48674926
    48684927            #remove the old mediafile that was run through mythtranscode
     
    49364995    elif file.attributes["type"].value=="recording":
    49374996        mediafile = file.attributes["filename"].value
    49384997    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)
    49404999    elif file.attributes["type"].value=="file":
    49415000        mediafile=file.attributes["filename"].value
    49425001    else:
     
    49885047                starttime = -1
    49895048                usecutlist = -1
    49905049
    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)
    49935051            mediafile = os.path.join(folder, 'newfile2.mpg')
    49945052        else:
    49955053            #we need to re-encode the file, make sure we get the right video/audio streams
     
    50155073                profile = defaultEncodingProfile
    50165074
    50175075            #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)
    50205077            mediafile = os.path.join(folder, 'newfile2.mpg')
    50215078
    50225079    #remove an intermediate file