Ticket #10071: mythburn.py_fixes.patch

File mythburn.py_fixes.patch, 37.1 KB (added by t.brackertz@…, 9 years ago)

backported fixes for 0.24-fixes-branch

  • (a) mythburn_0.24_fixes.git_4.10.2011.py vs. (b) mythburn_patched

    a b  
    11#!/usr/bin/env python
     2# -*- coding: utf-8 -*-
    23# mythburn.py
    34# The ported MythBurn scripts which feature:
    45
     
    3132#pyfribidi
    3233
    3334#Optional (alternate demuxer)
    34 #ProjectX - 0.90.4.00
     35#ProjectX >= 0.91
    3536
    3637#******************************************************************************
    3738#******************************************************************************
    3839#******************************************************************************
    3940
     41
     42# All strings in this file should be unicode, not byte string!! They get converted to utf-8 only
     43
     44
     45
     46
    4047# version of script - change after each update
    4148VERSION="0.1.20101206-1"
    4249
     
    239246
    240247def write(text, progress=True):
    241248    """Simple place to channel all text output through"""
     249
     250    text = text.encode("utf-8", "replace")
    242251    sys.stdout.write(text + "\n")
    243252    sys.stdout.flush()
    244253
     
    253262    """Display an error message and exit app"""
    254263    write("*"*60)
    255264    write("ERROR: " + msg)
     265    write("See mythburn.log for more information.")
    256266    write("*"*60)
    257267    write("")
    258268    saveSetting("MythArchiveLastRunResult", "Failed: " + quoteString(msg));
     
    287297# Try to work out how many cpus we have available
    288298
    289299def getCPUCount():
    290     """return the number of CPU's"""
     300    """return the number of CPUs"""
    291301    cpustat = open("/proc/cpuinfo")
    292302    cpudata = cpustat.readlines()
    293303    cpustat.close()
     
    349359
    350360def getDatabaseConnection():
    351361    """Returns a mySQL connection to mythconverg database."""
    352     return MySQLdb.connect(host=mysql_host, user=mysql_user, passwd=mysql_passwd, db=mysql_db, init_command='SET NAMES utf8')
     362    return MySQLdb.connect(host=mysql_host, user=mysql_user, passwd=mysql_passwd, db=mysql_db, charset="utf8", use_unicode=True)
    353363
    354364#############################################################
    355365# Returns true/false if a given file or path exists.
     
    359369    return os.path.exists( file )
    360370
    361371#############################################################
    362 # Escape quotes in a filename
     372# Escape quotes in a command line argument
    363373
    364 def quoteFilename(filename):
    365     filename = filename.replace('"', '\\"')
    366     filename = filename.replace('`', '\\`')
    367     return '"%s"' % filename
     374def quoteCmdArg(arg):
     375    arg = arg.replace('"', '\\"')
     376    arg = arg.replace('`', '\\`')
     377    return '"%s"' % arg
    368378
    369379#############################################################
    370380# Returns the text contents from a given XML element.
     
    436446                os.remove(os.path.join(root, name))
    437447
    438448#############################################################
     449# Romoves all the objects from a directory
     450
     451def deleteEverythingInFolder(folder):
     452    for root, dirs, files in os.walk(folder, topdown=False):
     453        for name in files:
     454                os.remove(os.path.join(root, name))
     455        for name in dirs:
     456                if os.path.islink(os.path.join(root, name)):
     457                    os.remove(os.path.join(root, name))
     458                else:
     459                    os.rmdir(os.path.join(root, name))
     460
     461#############################################################
    439462# Check to see if the user has cancelled the DVD creation process
    440463
    441464def checkCancelFlag():
     
    454477def runCommand(command):
    455478    checkCancelFlag()
    456479
    457     result = os.system(command)
     480    # mytharchivehelper needes this locale to work correctly
     481    try:
     482       oldlocale = os.environ["LC_ALL"]
     483    except:
     484       oldlocale = ""
     485    os.putenv("LC_ALL", "en_US.UTF-8")
     486    result = os.system(command.encode('utf-8'))
     487    os.putenv("LC_ALL", oldlocale)
    458488
    459489    if os.WIFEXITED(result):
    460490        result = os.WEXITSTATUS(result)
     
    485515
    486516    totalframes=int(musiclength * framespersecond)
    487517
    488     command = path_jpeg2yuv[0] + " -n %s -v0 -I p -f %s -j '%s' | %s -b 5000 -a %s -v 1 -f 8 -o '%s'" \
    489               % (totalframes, framespersecond, background, path_mpeg2enc[0], aspectratio, tempvideo)
     518    command = quoteCmdArg(path_jpeg2yuv[0]) + " -n %s -v0 -I p -f %s -j %s | %s -b 5000 -a %s -v 1 -f 8 -o %s" \
     519              % (totalframes, framespersecond, quoteCmdArg(background), quoteCmdArg(path_mpeg2enc[0]), aspectratio, quoteCmdArg(tempvideo))
    490520    result = runCommand(command)
    491521    if result<>0:
    492522        fatalError("Failed while running jpeg2yuv - %s" % command)
    493523
    494     command = path_mplex[0] + " -f 8 -v 0 -o '%s' '%s' '%s'" % (tempmovie, tempvideo, music)
     524    command = quoteCmdArg(path_mplex[0]) + " -f 8 -v 0 -o %s %s %s" % (quoteCmdArg(tempmovie), quoteCmdArg(tempvideo), quoteCmdArg(music))
    495525    result = runCommand(command)
    496526    if result<>0:
    497527        fatalError("Failed while running mplex - %s" % command)
    498528
    499529    if xmlfile != "":
    500         command = path_spumux[0] + " -m dvd -s 0 '%s' < '%s' > '%s'" % (xmlfile, tempmovie, finaloutput)
     530        command = quoteCmdArg(path_spumux[0]) + " -m dvd -s 0 %s < %s > %s" % (quoteCmdArg(xmlfile), quoteCmdArg(tempmovie), quoteCmdArg(finaloutput))
    501531        result = runCommand(command)
    502532        if result<>0:
    503533            fatalError("Failed while running spumux - %s" % command)
     
    15091539            top_element.appendChild(node)   
    15101540
    15111541            node = infoDOM.createElement("title")
    1512             node.appendChild(infoDOM.createTextNode(unicode(record[6], "UTF-8")))
     1542            node.appendChild(infoDOM.createTextNode(record[6]))
    15131543            top_element.appendChild(node)
    15141544
    15151545            #date time is returned as 2005-12-19 00:15:00           
     
    15231553            top_element.appendChild(node)   
    15241554
    15251555            node = infoDOM.createElement("subtitle")
    1526             node.appendChild(infoDOM.createTextNode(unicode(record[5], "UTF-8")))
     1556            node.appendChild(infoDOM.createTextNode(record[5]))
     1557
    15271558            top_element.appendChild(node)   
    15281559
    15291560            node = infoDOM.createElement("description")
    1530             node.appendChild(infoDOM.createTextNode(unicode(record[4], "UTF-8")))
     1561            node.appendChild(infoDOM.createTextNode(record[4]))
     1562
    15311563            top_element.appendChild(node)   
    15321564
    15331565            node = infoDOM.createElement("rating")
     
    16301662            top_element.appendChild(node)   
    16311663
    16321664            node = infoDOM.createElement("title")
    1633             node.appendChild(infoDOM.createTextNode(unicode(record[0], "UTF-8")))
     1665            node.appendChild(infoDOM.createTextNode(record[0]))
    16341666            top_element.appendChild(node)   
    16351667
    16361668            node = infoDOM.createElement("recordingdate")
     
    16471679            top_element.appendChild(node)   
    16481680
    16491681            node = infoDOM.createElement("subtitle")
    1650             node.appendChild(infoDOM.createTextNode(unicode(record[9], "UTF-8")))
     1682            node.appendChild(infoDOM.createTextNode(record[9]))
    16511683            top_element.appendChild(node)   
    16521684
    16531685            node = infoDOM.createElement("description")
    16541686            if record[2] != None:
    1655                 desc = unicode(record[2], "UTF-8")
     1687                desc = record[2]
    16561688                if desc != "None":
    16571689                    node.appendChild(infoDOM.createTextNode(desc))
    16581690                else:
     
    18051837def encodeAudio(format, sourcefile, destinationfile, deletesourceafterencode):
    18061838    write( "Encoding audio to "+format)
    18071839    if format == "ac3":
    1808         cmd = path_ffmpeg[0] + " -v 0 -y "
     1840        cmd = quoteCmdArg(path_ffmpeg[0]) + " -v 0 -y "
    18091841
    18101842        if cpuCount > 1:
    18111843            cmd += "-threads %d " % cpuCount
    18121844
    1813         cmd += "-i '%s' -f ac3 -ab 192k -ar 48000 '%s'" % (sourcefile, destinationfile)
     1845        cmd += "-i %s -f ac3 -ab 192k -ar 48000 %s" % (quoteCmdArg(sourcefile), quoteCmdArg(destinationfile))
    18141846        result = runCommand(cmd)
    18151847
    18161848        if result != 0:
     
    19041936    # run spumux to add subtitles if they exist
    19051937    if os.path.exists(os.path.dirname(destination) + "/stream.d/spumux.xml"):
    19061938        write("Checking integrity of subtitle pngs")
    1907         command = os.path.join(scriptpath, "testsubtitlepngs.sh") + " %s/stream.d/spumux.xml" % (os.path.dirname(destination))
     1939        command = quoteCmdArg(os.path.join(scriptpath, "testsubtitlepngs.sh")) + " " + quoteCmdArg(os.path.dirname(destination) + "/stream.d/spumux.xml")
    19081940        result = runCommand(command)
    19091941        if result<>0:
    19101942            fatalError("Failed while running testsubtitlepngs.sh - %s" % command)
    19111943
    19121944        write("Running spumux to add subtitles")
    1913         command = path_spumux[0] + " -P %s/stream.d/spumux.xml <%s >%s" % (os.path.dirname(destination), destination, os.path.splitext(destination)[0] + "-sub.mpg")
     1945        command = quoteCmdArg(path_spumux[0]) + " -P %s <%s >%s" % (quoteCmdArg(os.path.dirname(destination) + "/stream.d/spumux.xml"), quoteCmdArg(destination), quoteCmdArg(os.path.splitext(destination)[0] + "-sub.mpg"))
    19141946        result = runCommand(command)
    19151947        if result<>0:
    19161948            nonfatalError("Failed while running spumux.\n"
     
    19281960
    19291961def getStreamInformation(filename, xmlFilename, lenMethod):
    19301962    """create a stream.xml file for filename"""
    1931     filename = quoteFilename(filename)
    1932     command = "mytharchivehelper -i %s %s %d" % (filename, xmlFilename, lenMethod)
     1963
     1964    command = "mytharchivehelper -i %s %s %d" % (quoteCmdArg(filename), quoteCmdArg(xmlFilename), lenMethod)
     1965    write(command)
    19331966
    19341967    result = runCommand(command)
    19351968
     
    19381971
    19391972    # print out the streaminfo.xml file to the log
    19401973    infoDOM = xml.dom.minidom.parse(xmlFilename)
    1941     write("streaminfo.xml :-\n" + infoDOM.toprettyxml("    ", ""), False)
     1974    write(xmlFilename + ":-\n" + infoDOM.toprettyxml("    ", ""), False)
    19421975
    19431976#############################################################
    19441977# Gets the video width and height from a file's stream xml file
     
    19712004    """Use mythtranscode to cut commercials and/or clean up an mpeg2 file"""
    19722005
    19732006    if localfile != "":
    1974         localfile = quoteFilename(localfile)
    19752007        if usecutlist == True:
    1976             command = "mythtranscode --mpeg2 --honorcutlist -i %s -o %s" % (localfile, destination)
     2008            command = "mythtranscode --mpeg2 --honorcutlist -i %s -o %s" % (quoteCmdArg(localfile), quoteCmdArg(destination))
    19772009        else:
    1978             command = "mythtranscode --mpeg2 -i %s -o %s" % (localfile, destination)
     2010            command = "mythtranscode --mpeg2 -i %s -o %s" % (quoteCmdArg(localfile), quoteCmdArg(destination))
    19792011    else:
    19802012        if usecutlist == True:
    1981             command = "mythtranscode --mpeg2 --honorcutlist -c %s -s %s -o %s" % (chanid, starttime, destination)
     2013            command = "mythtranscode --mpeg2 --honorcutlist -c %s -s %s -o %s" % (chanid, starttime, quoteCmdArg(destination))
    19822014        else:
    1983             command = "mythtranscode --mpeg2 -c %s -s %s -o %s" % (chanid, starttime, destination)
     2015            command = "mythtranscode --mpeg2 -c %s -s %s -o %s" % (chanid, starttime, quoteCmdArg(destination))
    19842016
    19852017    result = runCommand(command)
    19862018
     
    20432075            write("Failed to generate Project-X cutlist.")
    20442076            return False
    20452077
    2046     pxbasename = os.path.splitext(os.path.basename(file))[0]
    2047 
    20482078    if os.path.exists(file) != True:
    20492079        write("Error: input file doesn't exist on local filesystem")
    20502080        return False
    20512081
    2052 
    2053     qdestdir = quoteFilename(folder)
    2054     qpxbasename = quoteFilename(pxbasename)
    2055     qfile = quoteFilename(file)
    2056     qcutlist = os.path.join(folder, "cutlist_x.txt")
    2057 
    2058     command = path_projectx[0] + " -id %s" % getStreamList(folder)
     2082    command = quoteCmdArg(path_projectx[0]) + " %s -id '%s' -set ExternPanel.appendPidToFileName=1 -out %s -name stream" % (quoteCmdArg(file), getStreamList(folder), quoteCmdArg(folder))
    20592083    if usecutlist == True:
    2060         command += " -cut %s -out %s -name %s %s" % (qcutlist, qdestdir, qpxbasename, qfile)
    2061     else:
    2062         command += " -out %s -name %s %s" % (qdestdir, qpxbasename, qfile)
    2063 
     2084        command += " -cut %s" % quoteCmdArg(os.path.join(folder, "cutlist_x.txt"))
    20642085    write(command)
    20652086
    20662087    result = runCommand(command)
     
    20722093
    20732094
    20742095    # workout which files we need and rename them
    2075     renameProjectXFiles(folder, pxbasename)
     2096    video, audio1, audio2 = selectStreams(folder)
     2097    if addSubtitles:
     2098        subtitles = selectSubtitleStream(folder)
     2099
     2100    videoID_hex = "0x%x" % video[VIDEO_ID]
     2101    if audio1[AUDIO_ID] != -1:
     2102        audio1ID_hex = "0x%x" % audio1[AUDIO_ID]
     2103    else:
     2104        audio1ID_hex = ""
     2105    if audio2[AUDIO_ID] != -1:
     2106        audio2ID_hex = "0x%x" % audio2[AUDIO_ID]
     2107    else:
     2108        audio2ID_hex = ""
     2109    if addSubtitles and subtitles[SUBTITLE_ID] != -1:
     2110        subtitlesID_hex = "0x%x" % subtitles[SUBTITLE_ID]
     2111    else:
     2112        subtitlesID_hex = ""
     2113
     2114
     2115    files = os.listdir(folder)
     2116    for file in files:
     2117        if file[0:9] == "stream{0x": # don't rename files that have already been renamed
     2118            PID = file[7:13]
     2119            SubID = file[19:23]
     2120            if PID == videoID_hex or SubID == videoID_hex:
     2121                os.rename(os.path.join(folder, file), os.path.join(folder, "stream.mv2"))
     2122            elif PID == audio1ID_hex or SubID == audio1ID_hex:
     2123                os.rename(os.path.join(folder, file), os.path.join(folder, "stream0." + file[-3:]))
     2124            elif PID == audio2ID_hex or SubID == audio2ID_hex:
     2125                os.rename(os.path.join(folder, file), os.path.join(folder, "stream1." + file[-3:]))
     2126            elif PID == subtitlesID_hex or SubID == subtitlesID_hex:
     2127                if file[-3:] == "sup":
     2128                    os.rename(os.path.join(folder, file), os.path.join(folder, "stream.sup"))
     2129                else:
     2130                    os.rename(os.path.join(folder, file), os.path.join(folder, "stream.sup.IFO"))
     2131
     2132
     2133    # Fallback if assignment and renaming by ID failed
     2134   
     2135    files = os.listdir(folder)
     2136    for file in files:
     2137        if file[0:9] == "stream{0x": # don't rename files that have already been renamed
     2138            if not os.path.exists(os.path.join(folder, "stream.mv2")) and file[-3:] == "m2v":
     2139                os.rename(os.path.join(folder, file), os.path.join(folder, "stream.mv2"))
     2140            elif not (os.path.exists(os.path.join(folder, "stream0.ac3")) or os.path.exists(os.path.join(folder, "stream0.mp2"))) and file[-3:] == "ac3":
     2141                os.rename(os.path.join(folder, file), os.path.join(folder, "stream0.ac3"))
     2142            elif not (os.path.exists(os.path.join(folder, "stream0.ac3")) or os.path.exists(os.path.join(folder, "stream0.mp2"))) and file[-3:] == "mp2":
     2143                os.rename(os.path.join(folder, file), os.path.join(folder, "stream0.mp2"))
     2144            elif not (os.path.exists(os.path.join(folder, "stream1.ac3")) or os.path.exists(os.path.join(folder, "stream1.mp2"))) and file[-3:] == "ac3":
     2145                os.rename(os.path.join(folder, file), os.path.join(folder, "stream1.ac3"))
     2146            elif not (os.path.exists(os.path.join(folder, "stream1.ac3")) or os.path.exists(os.path.join(folder, "stream1.mp2"))) and file[-3:] == "mp2":
     2147                os.rename(os.path.join(folder, file), os.path.join(folder, "stream1.mp2"))
     2148            elif not os.path.exists(os.path.join(folder, "stream.sup")) and file[-3:] == "sup":
     2149                os.rename(os.path.join(folder, file), os.path.join(folder, "stream.sup"))
     2150            elif not os.path.exists(os.path.join(folder, "stream.sup.IFO")) and file[-3:] == "IFO":
     2151                os.rename(os.path.join(folder, file), os.path.join(folder, "stream.sup.IFO"))
     2152
    20762153
    20772154    # if we have some dvb subtitles and the user wants to add them to the DVD
    20782155    # convert them to pngs and create the spumux xml file
     
    20812158            os.path.exists(os.path.join(folder, "stream.sup.IFO"))):
    20822159            write("Found DVB subtitles converting to DVD subtitles")
    20832160            command = "mytharchivehelper --sup2dast "
    2084             command += " %s %s 0" % (os.path.join(folder, "stream.sup"), os.path.join(folder, "stream.sup.IFO"))
     2161            command += " %s %s 0" % (quoteCmdArg(os.path.join(folder, "stream.sup")), quoteCmdArg(os.path.join(folder, "stream.sup.IFO")))
    20852162
    20862163            result = runCommand(command)
    20872164
     
    20962173    return True
    20972174
    20982175#############################################################
    2099 # find the required stream files and rename them
    2100 
    2101 def renameProjectXFiles(folder, pxbasename):
    2102 
    2103     write("renameProjectXFiles start -----------------------------------------", False)
    2104     logf = open(os.path.join(folder, pxbasename + "_log.txt"))
    2105     logdata = logf.readlines()
    2106     logf.close()
    2107 
    2108     # find stream PIDs and Files
    2109     streamIds = []
    2110     streamFiles = []   
    2111     for line in logdata:
    2112          tokens = line.split()
    2113          if len(tokens) > 0:
    2114             if tokens[0] == "++>":
    2115               # From ProjectX/resources/pjxresources_en.properties:
    2116               if tokens[1] == "Mpg":
    2117                 if tokens[2] == "Video:":
    2118                   write("found MPEG video stream %s" % tokens[4], False)
    2119                   streamIds.append(int(tokens[4], 16))
    2120                 if tokens[2] == "Audio:":
    2121                   write("found MPEG audio stream %s" % tokens[4], False)
    2122                   streamIds.append(int(tokens[4], 16))
    2123               if tokens[1] == "AC3/DTS":
    2124                 write("found AC3/DTS audio stream %s" % tokens[4], False)
    2125                 streamIds.append(int(tokens[4], 16))             
    2126               if tokens[1] == "LPCM":
    2127                 write("found LPCM audio stream %s" % tokens[4], False)
    2128                 streamIds.append(int(tokens[4], 16))             
    2129               if tokens[1] == "Teletext:":
    2130                 write("found Teletext stream %s" % tokens[3], False)
    2131                 streamIds.append(int(tokens[3], 16))             
    2132               if tokens[1] == "Subpicture:":
    2133                 write("found Subpicture stream %s" % tokens[3], False)
    2134                 streamIds.append(int(tokens[3], 16))
    2135               if tokens[1] == "Generic_VBI:":
    2136                 write("found Generic_VBI stream %s" % tokens[3], False)
    2137                 streamIds.append(int(tokens[3], 16))                                           
    2138             if tokens[0] == "--->":
    2139               if tokens[1] == "new":
    2140                 if tokens[2] == "File:":
    2141                   write("found file for stream 0x%x, %s" % (streamIds[len(streamIds)-1], tokens[3]), False)
    2142                   streamFiles.append(tokens[3].replace("'","")) # let's hope the path never has a space in it
    2143             if tokens[0] == "-->":
    2144               if tokens[1] == "stream":
    2145                 if tokens[2] == "omitted":
    2146                   write("stream 0x%x omitted" % streamIds[len(streamIds)-1], False)
    2147                   streamFiles.append("")
    2148                  
    2149     write("streadmIds=%s" % streamIds)
    2150     write("streamFiles=%s" % streamFiles)
    2151 
    2152     # choose which streams we need
    2153     video, audio1, audio2 = selectStreams(folder)
    2154 
    2155     if getFileType(folder) == "mpeg":
    2156         videoID  = video[VIDEO_ID] & 255
    2157         audio1ID = audio1[AUDIO_ID] & 255
    2158         audio2ID = audio2[AUDIO_ID] & 255
    2159     else:
    2160         videoID  = video[VIDEO_ID]
    2161         audio1ID = audio1[AUDIO_ID]
    2162         audio2ID = audio2[AUDIO_ID]
    2163 
    2164     # sanity check - we should have a file for each ID
    2165     if len(streamIds) == len(streamFiles):
    2166         # loop thought the available streams looking for the ones we want
    2167         for stream in streamIds:
    2168             write("got stream: %d" % stream, False)
    2169             if stream == videoID:
    2170                 write("found video streamID", False)
    2171                 if os.path.exists(streamFiles[streamIds.index(stream)]):
    2172                     write("found video stream file", False)
    2173                     os.rename(streamFiles[streamIds.index(stream)], os.path.join(folder, "stream.mv2"))
    2174 
    2175             if stream == audio1ID:
    2176                 write("found audio1 streamID", False)
    2177                 if os.path.exists(streamFiles[streamIds.index(stream)]):
    2178                     write("found audio1 stream file", False)
    2179                     if audio1[AUDIO_CODEC] == "AC3":
    2180                         os.rename(streamFiles[streamIds.index(stream)], os.path.join(folder, "stream0.ac3"))
    2181                     else:
    2182                         os.rename(streamFiles[streamIds.index(stream)], os.path.join(folder, "stream0.mp2"))
    2183 
    2184             if stream == audio2ID:
    2185                 write("found audio2 streamID", False)
    2186                 if os.path.exists(streamFiles[streamIds.index(stream)]):
    2187                     write("found audio2 stream file", False)
    2188                     if audio2[AUDIO_CODEC] == "AC3":
    2189                         os.rename(streamFiles[streamIds.index(stream)], os.path.join(folder, "stream1.ac3"))
    2190                     else:
    2191                         os.rename(streamFiles[streamIds.index(stream)], os.path.join(folder, "stream1.mp2"))
    2192 
    2193     # final chance to find the correct stream files
    2194     if not os.path.exists(os.path.join(folder, "stream.mv2")):
    2195         if os.path.exists(os.path.join(folder, pxbasename + ".m2v")):
    2196             os.rename(os.path.join(folder, pxbasename + ".m2v"), os.path.join(folder, "stream.mv2"))
    2197 
    2198     if not os.path.exists(os.path.join(folder, "stream0.mp2")) or not os.path.exists(os.path.join(folder, "stream0.ac3")):
    2199         if os.path.exists(os.path.join(folder, pxbasename + ".mp2")):
    2200             os.rename(os.path.join(folder, pxbasename + ".mp2"), os.path.join(folder, "stream0.mp2"))
    2201         if os.path.exists(os.path.join(folder, pxbasename + ".ac3")):
    2202             os.rename(os.path.join(folder, pxbasename + ".ac3"), os.path.join(folder, "stream0.ac3"))
    2203 
    2204     if not os.path.exists(os.path.join(folder, "stream1.mp2")) or not os.path.exists(os.path.join(folder, "stream1.ac3")):
    2205         if os.path.exists(os.path.join(folder, pxbasename + "[1].mp2")):
    2206             os.rename(os.path.join(folder, pxbasename + "[1].mp2"), os.path.join(folder, "stream1.mp2"))
    2207         if os.path.exists(os.path.join(folder, pxbasename + "[1].ac3")):
    2208             os.rename(os.path.join(folder, pxbasename + "[1].ac3"), os.path.join(folder, "stream1.ac3"))
    2209 
    2210     # do we have any subtitle files
    2211     if os.path.exists(os.path.join(folder, pxbasename + ".sup")):
    2212         os.rename(os.path.join(folder, pxbasename + ".sup"), os.path.join(folder, "stream.sup"))
    2213 
    2214     if os.path.exists(os.path.join(folder, pxbasename + ".sup.IFO")):
    2215         os.rename(os.path.join(folder, pxbasename + ".sup.IFO"), os.path.join(folder, "stream.sup.IFO"))
    2216 
    2217     write("renameProjectXFiles end -----------------------------------------", False)
    2218 
    2219 #############################################################
    22202176# convert time stamp to pts
    22212177
    22222178def ts2pts(time):
     
    22862242            fr=frameratePAL
    22872243        else:
    22882244            fr=framerateNTSC
    2289 
    2290         source = quoteFilename(source)
    2291 
    2292         command = "mytharchivehelper -t  %s '%s' %s" % (source, seconds, destination)
     2245        command = "mytharchivehelper -t %s '%s' %s" % (quoteCmdArg(source), seconds, quoteCmdArg(destination))
    22932246        result = runCommand(command)
    22942247        if result <> 0:
    22952248            fatalError("Failed while running mytharchivehelper to get thumbnails.\n"
     
    23122265    write("Extracting thumbnail images from: %s - at %s" % (source, thumbList))
    23132266    write("Destination file %s" % destination)
    23142267
    2315     source = quoteFilename(source)
    2316 
    2317     command = "mytharchivehelper -v important -t %s '%s' %s" % (source, thumbList, destination)
     2268    command = "mytharchivehelper -v important -t %s '%s' %s" % (quoteCmdArg(source), thumbList, quoteCmdArg(destination))
     2269    write(command)
    23182270    result = runCommand(command)
    23192271    if result <> 0:
    23202272        fatalError("Failed while running mytharchivehelper to get thumbnails")
     
    23292281
    23302282    passes = int(getText(profileNode.getElementsByTagName("passes")[0]))
    23312283
    2332     command = path_ffmpeg[0]
     2284    command = quoteCmdArg(path_ffmpeg[0])
    23332285
    23342286    if cpuCount > 1:
    23352287        command += " -threads %d" % cpuCount
     
    23422294
    23432295        # do some parameter substitution
    23442296        if value == "%inputfile":
    2345             value = quoteFilename(source)
     2297            value = quoteCmdArg(source)
    23462298        if value == "%outputfile":
    2347             value = quoteFilename(destvideofile)
     2299            value = quoteCmdArg(destvideofile)
    23482300        if value == "%aspect":
    23492301            value = aspectratio
    23502302
     
    23982350        passLog = os.path.join(getTempPath(), 'pass')
    23992351
    24002352        pass1 = string.replace(command, "%passno","1")
    2401         pass1 = string.replace(pass1, "%passlogfile", passLog)
     2353        pass1 = string.replace(pass1, "%passlogfile", quoteCmdArg(passLog))
    24022354        write("Pass 1 - " + pass1)
    24032355        result = runCommand(pass1)
    24042356
     
    24102362            os.remove(destvideofile)
    24112363
    24122364        pass2 = string.replace(command, "%passno","2")
    2413         pass2 = string.replace(pass2, "%passlogfile", passLog)
     2365        pass2 = string.replace(pass2, "%passlogfile", quoteCmdArg(passLog))
    24142366        write("Pass 2 - " + pass2)
    24152367        result = runCommand(pass2)
    24162368
     
    25152467    samplerate, channels = getAudioParams(folder)
    25162468    videores, fps, aspectratio = getVideoParams(folder)
    25172469
    2518     command =  path_ffmpeg[0] + " -y "
     2470    command =  quoteCmdArg(path_ffmpeg[0]) + " -y "
    25192471
    25202472    if cpuCount > 1:
    25212473        command += "-threads %d " % cpuCount
    25222474
    2523     command += "-f s16le -ar %s -ac %s -i %s " % (samplerate, channels, os.path.join(folder, "audout"))
     2475    command += "-f s16le -ar %s -ac %s -i %s " % (samplerate, channels, quoteCmdArg(os.path.join(folder, "audout")))
    25242476    command += "-f rawvideo -pix_fmt yuv420p -s %s -aspect %s -r %s " % (videores, aspectratio, fps)
    2525     command += "-i %s " % os.path.join(folder, "vidout")
     2477    command += "-i %s " % quoteCmdArg(os.path.join(folder, "vidout"))
    25262478    command += "-aspect %s -r %s " % (aspectratio, fps)
    25272479    if (deinterlace == 1):
    25282480        command += "-deinterlace "
     
    25302482    command += "-s %s -b %s -vcodec mpeg2video " % (outvideores, outvideobitrate)
    25312483    command += "-qmin %s -qmax %s -qdiff %s " % (qmin, qmax, qdiff)
    25322484    command += "-ab %s -ar %s -acodec %s " % (outaudiobitrate, outaudiosamplerate, outaudiocodec)
    2533     command += "-f dvd %s" % quoteFilename(destvideofile)
     2485    command += "-f dvd %s" % quoteCmdArg(destvideofile)
    25342486
    25352487    #wait for mythtranscode to create the fifos
    25362488    tries = 30
     
    25672519def CreateDVDISO(title):
    25682520    write("Creating ISO image")
    25692521    checkCancelFlag()
    2570     command = path_mkisofs[0] + ' -dvd-video '
    2571     command += ' -V ' + quoteFilename(title)
    2572     command += ' -o ' + os.path.join(getTempPath(), 'mythburn.iso')
    2573     command += " " + os.path.join(getTempPath(),'dvd')
     2522    command = quoteCmdArg(path_mkisofs[0]) + ' -dvd-video '
     2523    command += ' -V ' + quoteCmdArg(title)
     2524    command += ' -o ' + quoteCmdArg(os.path.join(getTempPath(), 'mythburn.iso'))
     2525    command += " " + quoteCmdArg(os.path.join(getTempPath(),'dvd'))
    25742526
    25752527    result = runCommand(command)
    25762528
     
    25852537
    25862538def BurnDVDISO(title):
    25872539    write( "Burning ISO image to %s" % dvddrivepath)
    2588     checkCancelFlag()
    25892540
    25902541    finished = False
    2591     tries = 0
    2592     while not finished and tries < 10:
    2593         f = os.open(dvddrivepath, os.O_RDONLY | os.O_NONBLOCK)
    2594         drivestatus = ioctl(f,CDROM.CDROM_DRIVE_STATUS, 0)
    2595         os.close(f);
     2542    while not finished:
     2543        # Maybe the user has no appropriate medium or something alike. Give her the chance to cancel.
     2544        checkCancelFlag()
    25962545
    2597         if drivestatus == CDROM.CDS_DISC_OK or drivestatus == CDROM.CDS_NO_INFO:
     2546        def drivestatus():
     2547            f = os.open(dvddrivepath, os.O_RDONLY | os.O_NONBLOCK)
     2548            return ioctl(f,CDROM.CDROM_DRIVE_STATUS, 0)
     2549            os.close(f);
     2550
     2551        # I drive needs some time (for example to close the tray) give it to it
     2552        tries = 0
     2553        while drivestatus() == CDROM.CDS_DRIVE_NOT_READY and tries < 6:
     2554            time.sleep(5)
     2555            tries += 1
     2556
     2557        if drivestatus() == CDROM.CDS_DISC_OK or drivestatus() == CDROM.CDS_NO_INFO:
    25982558
    25992559            # If the frontend has a previously burnt DVD+RW mounted,
    26002560            # growisofs will fail to burn it, so try to pumount it first...
    2601             runCommand("pumount " + dvddrivepath);
     2561            runCommand("pumount " + quoteCmdArg(dvddrivepath));
    26022562
    26032563            if mediatype == DVD_RW and erasedvdrw == True:
    26042564                command = path_growisofs[0] + " -dvd-compat "
     
    26182578            write("Running growisofs to burn DVD")
    26192579
    26202580            result = runCommand(command)
    2621             if result != 0:
     2581            if result == 0:
     2582                finished = True
     2583            elif result == 252:
     2584                write("-"*60)
     2585                write("You probably inserted a medium of wrong type.")
     2586                if mediatype == DVD_SL:
     2587                  write("Please insert an empty single-layer disc (DVD+R or DVD-R).")
     2588                if mediatype == DVD_DL:
     2589                  write("Please insert an empty double-layer disc (DVD+R DL or DVD-R DL).")
     2590                if mediatype == DVD_RW:
     2591                  write("Please insert a rewritable disc (DVD+RW or DVD-RW).")
     2592            else:
    26222593                write("-"*60)
    26232594                write("ERROR: Failed while running growisofs.")
    26242595                write("Result %d, Command was: %s" % (result, command))
    26252596                write("Please check the troubleshooting section of the README for ways to fix this error")
    26262597                write("-"*60)
    26272598                write("")
    2628                 sys.exit(1)
    2629             finished = True
     2599                write("Going to try it again until canceled by user:")
     2600                write("-"*60)
     2601                write("")
    26302602
    26312603            try:
    26322604                # eject the burned disc
     2605                runCommand("pumount " + quoteCmdArg(dvddrivepath));
    26332606                f = os.open(dvddrivepath, os.O_RDONLY | os.O_NONBLOCK)
    26342607                r = ioctl(f,CDROM.CDROMEJECT, 0)
    26352608                os.close(f)
     
    26372610                write("Failed to eject the disc! "
    26382611                      "Maybe the media monitor has mounted it")
    26392612
    2640         elif drivestatus == CDROM.CDS_TRAY_OPEN:
    2641             # Give the user 10secs to close the Tray
     2613        elif drivestatus() == CDROM.CDS_TRAY_OPEN:
    26422614            write("Waiting for tray to close.")
    2643             time.sleep(10)
    2644         elif drivestatus == CDROM.CDS_NO_DISC:
     2615            # Wait until user closes tray or cancels
     2616            while drivestatus() == CDROM.CDS_TRAY_OPEN:
     2617                checkCancelFlag()
     2618                time.sleep(5)
     2619        elif drivestatus() == CDROM.CDS_NO_DISC:
    26452620            # Open the Tray, if there is one.
    26462621            write("Opening tray to get it fed with a DVD.")
    26472622            f = os.open(dvddrivepath, os.O_RDONLY | os.O_NONBLOCK)
    26482623            ioctl(f,CDROM.CDROMEJECT, 0)
    26492624            os.close(f);
    2650         elif drivestatus == CDROM.CDS_DRIVE_NOT_READY:
    2651             # Try a hard reset
     2625        elif drivestatus() == CDROM.CDS_DRIVE_NOT_READY:
     2626            # Try a hard reset if the device is still not ready
    26522627            write("Trying a hard-reset of the device")
    26532628            f = os.open(dvddrivepath, os.O_RDONLY | os.O_NONBLOCK)
    26542629            ioctl(f,CDROM.CDROMEJECT, 0)
    26552630            os.close(f);
    26562631
    2657         time.sleep(1)
    2658         tries += 1
    2659 
    2660     if not finished:
    2661         fatalError("Tried 10 times to get a good status from DVD drive - Giving up!")
    26622632
    26632633    write("Finished burning ISO image")
    26642634
     
    26692639def deMultiplexMPEG2File(folder, mediafile, video, audio1, audio2):
    26702640
    26712641    if getFileType(folder) == "mpegts":
    2672         command = "mythreplex --demux --fix_sync -t TS -o %s " % (folder + "/stream")
     2642        command = "mythreplex --demux --fix_sync -t TS -o %s " % quoteCmdArg(folder + "/stream")
    26732643        command += "-v %d " % (video[VIDEO_ID])
    26742644
    26752645        if audio1[AUDIO_ID] != -1:
     
    26892659                command += "-c %d " % (audio2[AUDIO_ID])
    26902660
    26912661    else:
    2692         command = "mythreplex --demux --fix_sync -o %s " % (folder + "/stream")
     2662        command = "mythreplex --demux --fix_sync -o %s " % quoteCmdArg(folder + "/stream")
    26932663        command += "-v %d " % (video[VIDEO_ID] & 255)
    26942664
    26952665        if audio1[AUDIO_ID] != -1:
     
    27092679            elif audio2[AUDIO_CODEC] == 'EAC3':
    27102680                command += "-c %d " % (audio2[AUDIO_ID] & 255)
    27112681
    2712     mediafile = quoteFilename(mediafile)
     2682    mediafile = quoteCmdArg(mediafile)
    27132683    command += mediafile
    27142684    write("Running: " + command)
    27152685
     
    27262696    M2Vsize0 = os.path.getsize(source)
    27272697    write("Initial M2Vsize is %.2f Mb , target is %.2f Mb" % ( (float(M2Vsize0)/mega), (float(M2Vsize0)/(factor*mega)) ))
    27282698
    2729     command = path_M2VRequantiser[0]   
     2699    command = quoteCmdArg(path_M2VRequantiser[0])   
    27302700    command += " %.5f "  % factor
    27312701    command += " %s "  % M2Vsize0
    2732     command += " <  %s " % source
    2733     command += " >  %s " % destination
     2702    command += " <  %s " % quoteCmdArg(source)
     2703    command += " >  %s " % quoteCmdArg(destination)
    27342704 
    27352705    write("Running: " + command)
    27362706    result = runCommand(command)
     2707    if result<>0:
     2708        fatalError("Failed while running M2VRequantiser. Command was %s" % command)
    27372709
    27382710    M2Vsize1 = os.path.getsize(destination)
    27392711       
    27402712    write("M2Vsize after requant is  %.2f Mb " % (float(M2Vsize1)/mega))
    27412713    fac1=float(M2Vsize0) / float(M2Vsize1)
    27422714    write("Factor demanded %.5f, achieved %.5f, ratio %.5f " % ( factor, fac1, fac1/factor))
    2743            
    2744     if result<>0:
    2745         fatalError("Failed while running M2VRequantiser. Command was %s" % command)
    27462715
    27472716#############################################################
    27482717# Calculates the total size of all the video, audio and menu files
     
    35013470                height = getScaledAttribute(node, "h")
    35023471                frames = int(secondsToFrames(menulength))
    35033472
    3504                 command = "mytharchivehelper -t  %s '%s' '%s' %d" % (inputfile, starttime, outputfile, frames)
     3473                command = "mytharchivehelper -t %s '%s' '%s' %d" % (quoteCmdArg(inputfile), starttime, quoteCmdArg(outputfile), frames)
    35053474                result = runCommand(command)
    35063475                if (result != 0):
    35073476                    write( "mytharchivehelper failed with code %d. Command = %s" % (result, command) )
     
    43674336    # 2. if there is one or more stream(s) using the 2nd preferred language we use that
    43684337    # 3. if we still haven't found a stream we use the stream with the lowest PID
    43694338    # 4. we prefer ac3 over mp2
    4370     # 5. if there are more that one stream with the chosen language we use the one with the lowest PID
     4339    # 5. if there are more than one stream with the chosen language we use the one with the lowest PID
    43714340
    43724341    write("Preferred audio languages %s and %s" % (preferredlang1, preferredlang2))
    43734342
     
    50915060        tmpfile = node.attributes["filename"].value
    50925061        filename = os.path.basename(tmpfile)
    50935062
    5094         res = runCommand("mytharchivehelper -r " + quoteFilename(tmpfile))
     5063        res = runCommand("mytharchivehelper -r " + quoteCmdArg(tmpfile))
     5064
    50955065        if res == 2:
    50965066            # file is on a remote filesystem so copy it to a local file
    50975067            write("Copying file from " + tmpfile)
     
    51565126
    51575127        write( "Final DVD Video format will be " + videomode)
    51585128
    5159         #Ensure the destination dvd folder is empty
    5160         if doesFileExist(os.path.join(getTempPath(),"dvd")):
    5161             deleteAllFilesInFolder(os.path.join(getTempPath(),"dvd"))
    51625129
    51635130        #Loop through all the files
    51645131        files=media[0].getElementsByTagName("file")
     
    51685135
    51695136            if debug_secondrunthrough==False:
    51705137                #Delete all the temporary files that currently exist
    5171                 deleteAllFilesInFolder(getTempPath())
     5138                deleteEverythingInFolder(getTempPath())
    51725139
    51735140            #If User wants to, copy remote files to a tmp dir
    51745141            if copyremoteFiles==True:
    51755142                if debug_secondrunthrough==False:
    51765143                    localCopyFolder=os.path.join(getTempPath(),"localcopy")
    5177                     #If it already exists destroy it to remove previous debris
    5178                     if os.path.exists(localCopyFolder):
    5179                         #Remove all the files first
    5180                         deleteAllFilesInFolder(localCopyFolder)
    5181                         #Remove the folder
    5182                         os.rmdir (localCopyFolder)
    51835144                    os.makedirs(localCopyFolder)
    51845145                files=copyRemote(files,getTempPath())
    51855146
     
    51935154                folder=getItemTempPath(filecount)
    51945155
    51955156                if debug_secondrunthrough==False:
    5196                     #If it already exists destroy it to remove previous debris
    5197                     if os.path.exists(folder):
    5198                         #Remove all the files first
    5199                         deleteAllFilesInFolder(folder)
    5200                         subtitlefolder = os.path.join(folder, "stream.d")
    5201                         if os.path.exists(subtitlefolder):
    5202                             deleteAllFilesInFolder(subtitlefolder)
    5203                             os.rmdir(subtitlefolder)
    5204                         previewfolder = os.path.join(folder, "preview")
    5205                         if os.path.exists(previewfolder):
    5206                             deleteAllFilesInFolder(previewfolder)
    5207                             os.rmdir(previewfolder)
    5208                         #Remove the folder
    5209                         os.rmdir (folder)
    52105157                    os.makedirs(folder)
    52115158                #Do the pre-process work
    52125159                preProcessFile(node,folder,filecount)