Advertisement
Guest User

Untitled

a guest
May 19th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # HH:MM:SS.SSS - starts with 00:00:00.0000
  4.  
  5. import optparse
  6. import string
  7. import math
  8.  
  9. def generateMkvXml(line, chapterNum):
  10.     matroskaXml = "\t\t<ChapterAtom>\n"
  11.     matroskaXml += "\t\t\t<ChapterTimeStart>" + line.rstrip('\n') + "</ChapterTimeStart>\n"
  12.     matroskaXml += "\t\t\t<ChapterDisplay>\n"
  13.     matroskaXml += "\t\t\t\t<ChapterString>Chapter " + str(chapterNum) + "</ChapterString>\n"
  14.     matroskaXml += "\t\t\t\t<ChapterLanguage>eng</ChapterLanguage>\n"
  15.     matroskaXml += "\t\t\t</ChapterDisplay>\n"
  16.     matroskaXml += "\t\t</ChapterAtom>\n"
  17.     return matroskaXml  
  18.  
  19. def returnTime( ptsMark, offset ):
  20.     ptsFreq = 45000
  21.     ptsMark -= offset
  22.     ptsTime = float(ptsMark) / float(ptsFreq)
  23.     ptsHour = math.modf(ptsTime / 3600)
  24.     ptsMinute = math.modf(float(ptsHour[0]) * 60)
  25.     ptsSecond = ptsMinute[0] * 60
  26.    
  27.     if ptsSecond >= 10:
  28.         return '%(hour)02d:%(minute)02d:%(second)02.3F' % {'hour': ptsHour[1], 'minute': ptsMinute[1], 'second': ptsSecond}
  29.     else:
  30.         return '%(hour)02d:%(minute)02d:0%(second)02.3F' % {'hour': ptsHour[1], 'minute': ptsMinute[1], 'second': ptsSecond}
  31.  
  32.  
  33. def main():
  34.     p = optparse.OptionParser(description=' Deconstructs the MPLS file and converts the PTS information to create properly formatted XML chapter file for Matroska.  This program needs the MPLS file from the BluRay disc associated with the M2TS file(s) that you are processing.',
  35.                               prog='bdchapters',
  36.                               version='BluRay Chapter Converter 0.3',
  37.                               usage='%prog -i [inputfile] -o [outputfile]')
  38.     p.add_option('--input', '-i', action="store", help='the MPLS file from the BluRay disc', dest="inputfile")
  39.     p.add_option('--output', '-o', action="store", help='the output XML file', dest="outputfile")
  40.     (options, arguments) = p.parse_args()
  41.    
  42.     if options.inputfile == None:
  43.         p.error("no inputfile specified.")  
  44.     elif options.outputfile == None:
  45.         options.outputfile = options.inputfile + ".xml"
  46.    
  47.     print "\n"
  48.     print 'Input file: %s' % options.inputfile
  49.     print 'Output file: %s' % options.outputfile
  50.     print "\n"
  51.  
  52.     matroskaXmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<Chapters>\n\t<EditionEntry>\n"
  53.     matroskaXmlFooter = "\t</EditionEntry>\n</Chapters>"
  54.    
  55.     input = open(options.inputfile, 'rb')  
  56.     output = open(options.outputfile, 'w')
  57.    
  58.     output.write(matroskaXmlHeader)
  59.  
  60.     count = 0
  61.  
  62.     bytelist = []
  63.     ptsinfo = []
  64.    
  65.     input.seek(-14, 2)
  66.     for x in range(14):
  67.         bytelist.append(input.read(1))
  68.        
  69.     ptsinfo.append(ord(bytelist[4])*(256**3) + ord(bytelist[5])*(256**2) + ord(bytelist[6])*(256) + ord(bytelist[7]))
  70.  
  71.     while True:
  72.         input.seek(-28, 1)
  73.         bytelist = []
  74.         for x in range(14):
  75.             bytelist.append(input.read(1))
  76.  
  77.         if ord(bytelist[13]) != 0:
  78.             break
  79.  
  80.         ptsinfo.append(ord(bytelist[4])*(256**3) + ord(bytelist[5])*(256**2) + ord(bytelist[6])*(256) + ord(bytelist[7]))
  81.         if ptsinfo[-1] == ptsinfo[-2]:
  82.             ptsinfo.pop([-1])
  83.             break
  84.  
  85.     ptsOffset = ptsinfo[-1]
  86.     ptsinfo.sort()
  87.    
  88.     for x in ptsinfo:
  89.         count += 1
  90.         timeStamp = returnTime( x, ptsOffset )
  91.         output.write(generateMkvXml(timeStamp, count))
  92.  
  93.     output.write(matroskaXmlFooter)
  94.    
  95.     input.close()
  96.     output.close()
  97.  
  98.    
  99. if __name__ == '__main__':
  100.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement