Advertisement
Guest User

MKVtoTS

a guest
Feb 5th, 2012
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 13.94 KB | None | 0 0
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_Outfile=MKVtoTS.exe
  3. #AutoIt3Wrapper_Res_Fileversion=1.1.0.0
  4. #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
  5. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  6. #include <ButtonConstants.au3>
  7. #include <EditConstants.au3>
  8. #include <GUIConstantsEx.au3>
  9. #include <ProgressConstants.au3>
  10. #include <StaticConstants.au3>
  11. #include <WindowsConstants.au3>
  12. #include <Array.au3>
  13. #Include <File.au3>
  14. #include <Constants.au3>
  15. #Include <GuiEdit.au3>
  16.  
  17. $BatchMode = False
  18.  
  19. $TempPath = RegRead("HKCU\Software\MKVtoTS", "TempPath")
  20.  
  21. If $TempPath = "" Then
  22.     While 1
  23.         $TempPath = InputBox("Need Temp Path", "Please enter the directory to store temp files (demuxed video and audio)", @TempDir)
  24.         If $TempPath <> "" Then
  25.             If FileExists($TempPath) Then
  26.                 RegWrite("HKCU\Software\MKVtoTS", "TempPath", "REG_SZ", $TempPath)
  27.                 ExitLoop
  28.             Else
  29.                 MsgBox(262160,"Error","Path " & $TempPath & " does not exist")
  30.             EndIf
  31.         Else
  32.             If @error = 1 Then
  33.                 Exit
  34.             Else
  35.                 MsgBox(262160,"Error","Nothing entered")
  36.             EndIf
  37.         EndIf
  38.     WEnd
  39. EndIf
  40.  
  41. $LogPath = $TempPath & "\MKVtoTS.log"
  42.  
  43. $eac3to = RegRead("HKCU\Software\MKVtoTS", "eac3toPath")
  44.  
  45. If $eac3to = "" Then
  46.     While 1
  47.         ;$eac3to = InputBox("Need eac3to path", "Please enter the full path to eac3to.exe", @ProgramFilesDir)
  48.  
  49.         $eac3to = FileOpenDialog ( "Need eac3to path", @ProgramFilesDir, "exe files (*.exe)" , 1, "eac3to.exe")
  50.  
  51.         If $eac3to <> "" Then
  52.             If FileExists($eac3to) and StringRight($eac3to,10) = "eac3to.exe" Then
  53.                 _Log("eac3to path looks valid, commiting to registry...")
  54.                 _Log("Setting eac3to path to: " & $eac3to)
  55.                 RegWrite("HKCU\Software\MKVtoTS", "eac3toPath", "REG_SZ", $eac3to)
  56.                 ExitLoop
  57.             Else
  58.                 If Not FileExists($eac3to) Then
  59.                     MsgBox(262160,"Error","Path " & $eac3to & " does not exist")
  60.                 EndIf
  61.  
  62.                 If Not StringRight($eac3to,10) = "eac3to.exe" Then
  63.                     MsgBox(262160,"Error","Path to " & $eac3to & " does not end in 'eac3to.exe'")
  64.                 EndIf
  65.             EndIf
  66.         Else
  67.             If @error Then
  68.                 Exit
  69.             Else
  70.                 MsgBox(262160,"Error","Nothing entered")
  71.             EndIf
  72.         EndIf
  73.     WEnd
  74. EndIf
  75.  
  76. $TSMuxer = RegRead("HKCU\Software\MKVtoTS", "TSMuxerPath")
  77.  
  78. If $TSMuxer = "" or Not FileExists($TSMuxer) Then
  79.  
  80.     While 1
  81.         $TSMuxer = FileOpenDialog ( "Need TSMuxer path", @ProgramFilesDir, "exe files (*.exe)" , 1, "tsMuxeR.exe")
  82.  
  83.         If $TSMuxer <> "" Then
  84.             If FileExists($TSMuxer) and StringRight($TSMuxer,11) = "tsMuxeR.exe" Then
  85.                 _Log("TSMuxer path looks valid, commiting to registry...")
  86.                 _Log("Setting TSMuxer path to: " & $TSMuxer)
  87.                 RegWrite("HKCU\Software\MKVtoTS", "TSMuxerPath", "REG_SZ", $TSMuxer)
  88.                 ExitLoop
  89.             Else
  90.                 If Not FileExists($TSMuxer) Then
  91.                     MsgBox(262160,"Error","Path " & $TSMuxer & " does not exist")
  92.                 EndIf
  93.  
  94.                 If Not StringRight($eac3to,11) = "tsMuxeR.exe" Then
  95.                     MsgBox(262160,"Error","Path to " & $TSMuxer & " does not end in 'tsMuxeR.exe'")
  96.                 EndIf
  97.             EndIf
  98.         Else
  99.             If @error Then
  100.                 Exit
  101.             Else
  102.                 MsgBox(262160,"Error","Nothing entered")
  103.             EndIf
  104.         EndIf
  105.     WEnd
  106.  
  107. EndIf
  108.  
  109. Dim $szDrive, $szDir, $szFName, $szExt
  110.  
  111. If $CmdLine[0] <> 1 then
  112.     $BatchMode = True
  113.     $TotalJobs = 0
  114.     $CurrJobNo = 0
  115. Else
  116.     $SourceFile = $CmdLine[1]
  117.     $TotalJobs = 1
  118.     $CurrJobNo = 1
  119. EndIf
  120.  
  121. #Region ### START Koda GUI section ### Form=
  122. $Form1 = GUICreate("MKVtoTS", 629, 471, -1, -1, -1, $WS_EX_ACCEPTFILES)
  123. GUICtrlCreateLabel("Input Path:", 16, 16, 66, 17)
  124. GUICtrlCreateLabel("Output Path:", 16, 48, 66, 17)
  125. $Input_SourceFile = GUICtrlCreateInput("", 88, 14, 441, 21)
  126. If $BatchMode Then
  127.     GUICtrlSetState ($Input_SourceFile, $GUI_DROPACCEPTED )
  128. EndIf
  129. $Input_TargetFile = GUICtrlCreateInput("", 88, 44, 441, 21)
  130. GUICtrlSetState(-1, $GUI_DISABLE)
  131. $Button_Browse = GUICtrlCreateButton("Browse...", 536, 12, 75, 25, $WS_GROUP)
  132. GUICtrlCreateLabel("Current File:", 16, 88, 60, 17)
  133. $Label_CurrentFile = GUICtrlCreateLabel("...", 96, 88, 381, 17)
  134. GUICtrlCreateLabel("Current Status:", 16, 112, 74, 17)
  135. $Label_Status = GUICtrlCreateLabel("Idle...", 96, 112, 374, 17)
  136. $Group_eac3to = GUICtrlCreateGroup("eac3to output", 16, 144, 593, 249)
  137. $Edit_log = GUICtrlCreateEdit("", 25, 165, 574, 217)
  138. $Group_TSMuxerOutput = GUICtrlCreateGroup("TSMuxer Output", 16, 400, 593, 57)
  139. $Progress1 = GUICtrlCreateProgress(24, 424, 518, 17)
  140. $Label_TSMuxerPct = GUICtrlCreateLabel("00 %", 552, 424, 35, 17, $SS_CENTER)
  141. $Label_Framerate = GUICtrlCreateLabel("Framerate:", 496, 112, 54, 17)
  142. $Input_Framerate = GUICtrlCreateInput("N/A", 560, 110, 49, 21)
  143. GUISetState(@sw_show)
  144.  
  145. If Not $BatchMode Then
  146.     $arrSourceFile = _PathSplit($SourceFile, $szDrive, $szDir, $szFName, $szExt)
  147.     GUICtrlSetData($Input_SourceFile, $SourceFile)
  148.     GUICtrlSetData($Label_CurrentFile, $SourceFile)
  149.     GUICtrlSetData($Input_TargetFile, $szDrive & $szDir & $szFName & ".ts")
  150.     GUICtrlSetState($Input_SourceFile, $GUI_DISABLE)
  151.     GUICtrlSetState($Input_TargetFile, $GUI_DISABLE)
  152.  
  153.     _Remux($SourceFile, $szDrive & $szDir & $szFName & ".ts")
  154.     Exit
  155. EndIf
  156.  
  157.  
  158. #EndRegion ### END Koda GUI section ###
  159.  
  160. While 1
  161.     $nMsg = GUIGetMsg()
  162.     Switch $nMsg
  163.         Case $GUI_EVENT_CLOSE
  164.             Exit
  165.         Case $GUI_EVENT_DROPPED
  166.  
  167.             $SelDir = @GUI_DragFile
  168.  
  169.             GUICtrlSetData ($Input_SourceFile, "")
  170.             GUICtrlSetData ($Input_SourceFile, $SelDir)
  171.  
  172.             If $SelDir <> "" Then
  173.  
  174.                 GUICtrlSetData($Input_SourceFile,$SelDir)
  175.                 GUICtrlSetData($Input_TargetFile,$SelDir)
  176.                 $arrInputFiles = _FileListToArray($SelDir, "*.mkv", 1)
  177.  
  178.                 If @error = 4 Then
  179.                     MsgBox(262160,"TSMuxer","No MKV files found in selected directory")
  180.                 Else
  181.  
  182.                     $TotalJobs = $arrInputFiles[0]
  183.  
  184.                     For $x = 1 to $arrInputFiles[0]
  185.  
  186.                         $CurrJobNo = $x
  187.                         $arrMovieName = StringSplit($arrInputFiles[$x], ".mkv", 1)
  188.                         $MovieName = $arrMovieName[1]
  189.                         $OutputPath = $SelDir & "\" & $MovieName & ".ts"
  190.                         GUICtrlSetData($Label_CurrentFile, $SelDir & "\" & $arrInputFiles[$x])
  191.  
  192.                         _Remux($SelDir & "\" & $arrInputFiles[$x], $OutputPath)
  193.  
  194.                         GUICtrlSetData($Progress1, 0)
  195.                         GUICtrlSetData($Label_TSMuxerPct, "00 %")
  196.                         GUICtrlSetData($Edit_log, "")
  197.                         GUICtrlSetData($Input_Framerate, "")
  198.  
  199.                     Next
  200.  
  201.                     GUICtrlSetData($Label_Status, "Completed " & $TotalJobs & " jobs.")
  202.  
  203.                 EndIf
  204.  
  205.             EndIf
  206.  
  207.         Case $Button_Browse
  208.  
  209.             $SelDir = FileSelectFolder ( "TSMuxer", "", 2, @DesktopDir )
  210.  
  211.             If $SelDir <> "" Then
  212.  
  213.                 GUICtrlSetData($Input_SourceFile,$SelDir)
  214.                 GUICtrlSetData($Input_TargetFile,$SelDir)
  215.                 $arrInputFiles = _FileListToArray($SelDir, "*.mkv", 1)
  216.  
  217.                 If @error = 4 Then
  218.                     MsgBox(262160,"TSMuxer","No MKV files found in selected directory")
  219.                 Else
  220.  
  221.                     $TotalJobs = $arrInputFiles[0]
  222.  
  223.                     For $x = 1 to $arrInputFiles[0]
  224.  
  225.                         $CurrJobNo = $x
  226.                         $arrMovieName = StringSplit($arrInputFiles[$x], ".mkv", 1)
  227.                         $MovieName = $arrMovieName[1]
  228.                         $OutputPath = $SelDir & "\" & $MovieName & ".ts"
  229.  
  230.                         _Remux($SelDir & "\" & $arrInputFiles[$x], $OutputPath)
  231.  
  232.                         GUICtrlSetData($Progress1, 0)
  233.                         GUICtrlSetData($Label_TSMuxerPct, "00 %")
  234.                         GUICtrlSetData($Edit_log, "")
  235.                         GUICtrlSetData($Input_Framerate, "")
  236.  
  237.                     Next
  238.  
  239.                     GUICtrlSetData($Label_Status, "Completed " & $TotalJobs & " jobs.")
  240.  
  241.                 EndIf
  242.  
  243.             EndIf
  244.  
  245.     EndSwitch
  246. WEnd
  247.  
  248. Func _Remux($SourceFile, $OutputFile)
  249.  
  250.     _Log("Starting remux job.  Input file: " & $SourceFile)
  251.  
  252.     GUICtrlSetData($Label_Status, "(Job " & $CurrJobNo & "/" & $TotalJobs & ") Parsing file with eac3to...")
  253.  
  254.     $nbtstat = Run('"' & $eac3to & '" ' & '"' & $SourceFile & '"', $TempPath, @sw_hide,$STDERR_CHILD + $STDOUT_CHILD)
  255.  
  256.     $out = ''
  257.  
  258.     While 1
  259.         $line = StdoutRead($nbtstat)
  260.         If @error Then ExitLoop
  261.         If $line <> '' Then
  262.             $line = StringReplace($line, Chr(0x08), "")
  263.             $line = StringReplace( $line, "-------------------------------------------------------------------------------", "")
  264.             $line = StringReplace($line, "                                                                               ", "")
  265.             $out &= $line
  266.             _GUICtrlEdit_AppendText($Edit_log, $line)
  267.             Sleep(100)
  268.         EndIf
  269.     Wend
  270.  
  271.     _GUICtrlEdit_AppendText($Edit_log, "---------------------------------------------------------------------------------------------------")
  272.     _GUICtrlEdit_AppendText($Edit_log, @CRLF & " ")
  273.  
  274.     $FinalString = StringReplace($out, Chr(0x08), "")
  275.     _Log("eac3to analysis output:")
  276.     _Log($FinalString)
  277.  
  278.     $AudioType = "AC3"
  279.     $FixFrameRate = False
  280.     $eac3toVidCmd = "1:vid.h264"
  281.  
  282.     If StringInStr($FinalString, "The video bitstream is encoded in a non-standard framerate.") Then
  283.         _Log("The video bitstream is encoded in a non-standard framerate.")
  284.         $FixFrameRate = True
  285.     EndIf
  286.  
  287.     If StringInStr($FinalString, "2: AC3") Then
  288.         $eac3toAudCmd = '2:aud.ac3 -KeepDialNorm'
  289.     Else
  290.         $eac3toAudCmd = '2:aud.ac3'
  291.     EndIf
  292.  
  293.     If StringInStr($FinalString, "3: AC3") Then
  294.         $eac3toAudCmd = '3:aud.ac3 -KeepDialNorm'
  295.     EndIf
  296.  
  297.     If StringInStr($FinalString, "2: AC3") and StringInStr($FinalString, "3: AC3") Then
  298.         Select
  299.             Case StringInStr($FinalString, "2: AC3, English, 5.1 channels")
  300.                 $eac3toAudCmd = '2:aud.ac3 -KeepDialNorm'
  301.             Case StringInStr($FinalString, "3: AC3, English, 5.1 channels")
  302.                 $eac3toAudCmd = '3:aud.ac3 -KeepDialNorm'
  303.             Case StringInStr($FinalString, "2: AC3, German") and StringInStr($FinalString, "3: AC3, 2.0 channels")
  304.                 $eac3toAudCmd = '3:aud.ac3 -KeepDialNorm'
  305.             Case StringInStr($FinalString, "2: AC3, German") and StringInStr($FinalString, "3: AC3, 5.1 channels")
  306.                 $eac3toAudCmd = '3:aud.ac3 -KeepDialNorm'
  307.             Case Else
  308.                 $eac3toAudCmd = '2:aud.ac3 -KeepDialNorm'
  309.         EndSelect
  310.     EndIf
  311.  
  312.     If StringInStr($FinalString, "2: DTS") Then
  313.         $eac3toAudCmd = '2:aud.dts'
  314.         $AudioType = "DTS"
  315.     EndIf
  316.  
  317.     $MetaLine1 = "MUXOPT --no-pcr-on-video-pid --new-audio-pes --vbr  --vbv-len=500"
  318.     $MetaLine2 = ""
  319.     $MetaLine3 = ""
  320.  
  321.     Select
  322.         Case StringInStr($FinalString, ", 24p /1.001")
  323.  
  324.             $VideoFPS = "24"
  325.             GUICtrlSetData($Input_Framerate, "23.976")
  326.  
  327.             $MetaLine2 = 'V_MPEG4/ISO/AVC, "' & $TempPath & '\vid.h264"' & ', fps=23.976, insertSEI, contSPS '
  328.  
  329.             If $AudioType = "AC3" Then
  330.                 $MetaLine3 = 'A_AC3, "' & $TempPath & '\aud.ac3"'
  331.             Else
  332.                 $MetaLine3 = 'A_DTS, "' & $TempPath & '\aud.dts"'
  333.             EndIf
  334.  
  335.             If $FixFrameRate Then
  336.                 _Log("Will change video framerate to 23.976")
  337.                 $eac3toVidCmd = $eac3toVidCmd & " -changeTo23.976"
  338.             EndIf
  339.  
  340.  
  341.         Case StringInStr($FinalString, ", 25p") or StringInStr($FinalString, ", 50i") or StringInStr($FinalString, ", 50p")
  342.  
  343.             $VideoFPS = "25"
  344.             GUICtrlSetData($Input_Framerate, "25.00")
  345.  
  346.             $MetaLine2 = 'V_MPEG4/ISO/AVC, "' & $TempPath & '\vid.h264"' & ', fps=25, insertSEI, contSPS '
  347.  
  348.             If $AudioType = "AC3" Then
  349.                 $MetaLine3 = 'A_AC3, "' & $TempPath & '\aud.ac3"'
  350.             Else
  351.                 $MetaLine3 = 'A_DTS, "' & $TempPath & '\aud.dts"'
  352.             EndIf
  353.  
  354.             If $FixFrameRate Then
  355.                 _Log("Will change video framerate to 23.976")
  356.                 $eac3toVidCmd = $eac3toVidCmd & " -changeTo25.000"
  357.             EndIf
  358.  
  359.         Case StringInStr($FinalString, ", 30p /1.001")
  360.  
  361.             GUICtrlSetData($Input_Framerate, "29.97")
  362.  
  363.             $MetaLine2 = 'V_MPEG4/ISO/AVC, "' & $TempPath & '\vid.h264"' & ', fps=29.970, insertSEI, contSPS '
  364.  
  365.             If $AudioType = "AC3" Then
  366.                 $MetaLine3 = 'A_AC3, "' & $TempPath & '\aud.ac3"'
  367.             Else
  368.                 $MetaLine3 = 'A_DTS, "' & $TempPath & '\aud.dts"'
  369.             EndIf
  370.  
  371.         Case Else
  372.             MsgBox(262160,"TSMuxer Error","Unrecognised video framerate")
  373.             Exit
  374.     EndSelect
  375.  
  376.     $TSMuxerMetaFile = $TempPath & "\TSM.META"
  377.  
  378.     $MetFile = FileOpen($TSMuxerMetaFile, 2)
  379.  
  380.     If $MetFile = -1 Then
  381.         MsgBox(262160,"Error","Error opening TSMuxer meta file for writing.  Path: " & $TSMuxerMetaFile)
  382.         Exit
  383.     EndIf
  384.  
  385.     $WriteMetaFile = FileWriteLine($MetFile, $MetaLine1)
  386.     FileWriteLine($MetFile, $MetaLine2)
  387.     FileWriteLine($MetFile, $MetaLine3)
  388.     FileClose($MetFile)
  389.  
  390.     $DemuxCmdLine = '"' & $eac3to & '" ' & '"' & $SourceFile & '" ' & $eac3toVidCmd & " " & $eac3toAudCmd
  391.     _Log("eac3to command line: " & $DemuxCmdLine)
  392.     _GUICtrlEdit_AppendText($Edit_log, "eac3to command line: " & $DemuxCmdLine)
  393.  
  394. ;~  If Not StringInStr($FinalString,"1 video track, 1 audio track") Then
  395. ;~      MsgBox(0,"Debug", "More than 1 vid or aud stream")
  396. ;~      Exit
  397. ;~  EndIf
  398.  
  399.     GUICtrlSetData($Label_Status, "(Job " & $CurrJobNo & "/" & $TotalJobs & ") Demuxing video and audio streams...")
  400.  
  401.     $nbtstat = Run($DemuxCmdLine, $TempPath, @sw_hide,$STDERR_CHILD + $STDOUT_CHILD)
  402.  
  403.     $out = ''
  404.  
  405.     While 1
  406.         $line = StdoutRead($nbtstat)
  407.         If @error Then ExitLoop
  408.         If $line <> '' Then
  409.             $line = StringReplace($line, Chr(0x08), "")
  410.             $line = StringReplace( $line, "-------------------------------------------------------------------------------", "")
  411.             $line = StringReplace($line, "                                                                               ", "")
  412.             $out &= $line
  413.             _GUICtrlEdit_AppendText($Edit_log, $line)
  414.             Sleep(100)
  415.         EndIf
  416.     Wend
  417.  
  418.     ;$FinalString = StringReplace($out, Chr(0x08), "")
  419.     ;GUICtrlSetData($Edit_log, $FinalString)
  420.  
  421.     GUICtrlSetData($Label_Status, "(Job " & $CurrJobNo & "/" & $TotalJobs & ") Muxing with TSMuxer...")
  422.     _Log("TSMuxer command line: " & '"' & $TSMuxer & '" ' & '"' & $TSMuxerMetaFile & '" "' & $OutputFile & '"')
  423.     ConsoleWrite(@ComSpec & " /c tsmuxer " & '"' & $TSMuxerMetaFile & '" "' & $OutputFile & '"' & @crlf)
  424.     $TSMuxerMux = Run('"' & $TSMuxer & '" ' & '"' & $TSMuxerMetaFile & '" "' & $OutputFile & '"', @TempDir, @sw_hide, $STDERR_CHILD + $STDOUT_CHILD)
  425.  
  426.     $out = ''
  427.  
  428.     While 1
  429.         $line = StdoutRead($TSMuxerMux)
  430.         If @error Then ExitLoop
  431.         If $line <> '' Then
  432.             If StringInStr($line, "% complete") Then
  433.                 $arrSplit = StringSplit($line, "% complete", 1)
  434.                 $CurrPer = $arrSplit[1]
  435.                 If Not StringInStr($CurrPer, "Processed") then
  436.                     GUICtrlSetData($Progress1, $CurrPer)
  437.                     GUICtrlSetData($Label_TSMuxerPct, $CurrPer & "%")
  438.                 EndIf
  439.             EndIf
  440.             $out &= $line
  441.         EndIf
  442.         Sleep(100)
  443.     Wend
  444.  
  445.     FileDelete($TempPath & "\aud.dts")
  446.     FileDelete($TempPath & "\aud.ac3")
  447.     FileDelete($TempPath & "\vid.h264")
  448.  
  449. EndFunc
  450.  
  451. Func _Log($LogString)
  452.     FileWriteLine($LogPath, $LogString)
  453. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement