Advertisement
Guest User

MKV DTS to AC3 Converter

a guest
Feb 19th, 2013
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf8 -*-
  3. # This script converts all DTS streams inside an MKV to AC3 and adds them to the MKV file
  4. # The newly generated MKV is stored in the target directory
  5.  
  6. # Import module OS
  7. import os, sys
  8.  
  9. param_count = len(sys.argv)
  10.  
  11. if param_count == 1:
  12.   print "mkvdts2ac3 - The DTS / DTS-HD to AC3 converter"
  13.   print
  14.   print "Usage: mkvdts2ac3 /path/to/my.movie.mkv"
  15.   quit()
  16.  
  17. # Get source MKV from MKV
  18. source_mkv = sys.argv[1]
  19.  
  20. # Target MKV = source MKV filename plus output path
  21. target_mkv = source_mkv[:-3] + "AC3.mkv"
  22.  
  23. # Get FPS of source MKV
  24. mkvinfo = os.popen ("mkvinfo " + source_mkv)
  25.  
  26. fps = ""
  27. video_track = 0
  28.  
  29. for info in mkvinfo:
  30.   if info.find ("Tracktyp: video") <> -1:
  31.     video_track = 1
  32.  
  33.   if video_track and info.find ("Standarddauer:") <> -1:
  34.     fps = info
  35.     video_track = 0
  36.  
  37. fps = fps.split("(")[1].split(" ")[0].strip()
  38.  
  39. # Get track information of source MKV
  40. tracks = os.popen ("mkvmerge -i " + source_mkv )
  41.  
  42. track_count = 1
  43.  
  44. ac3_tracks = []
  45.  
  46. # Loop through all tracks
  47. for track in tracks:
  48.   # Only process strings with "Track ID" in them
  49.   if track.find ('Track ID') <> -1:
  50.     # Remove whitespaces
  51.     track = track.strip()
  52.    
  53.     # Extract AC3 streams
  54.     if track.find ('A_AC3') <> -1:
  55.       extension = 'ac3'
  56.       extract_command = "nice -n 19 ionice -c3 mkvextract tracks " + source_mkv + " " + str (track_count) +":/tmp/Track_" + str (track_count) + "." + extension
  57.       print extract_command
  58.       os.popen ( extract_command )
  59.       # Remember generated AC3 stream for later remuxing
  60.       ac3_tracks.append ("/tmp/Track_" + str (track_count) + ".ac3")
  61.  
  62.     # Extract DTS / DTS-HD and convert to AC3
  63.     if track.find ('A_DTS') <> -1:
  64.       extension = 'dts'
  65.       # Extract DTS / DTS-HD stream from MKV
  66.       extract_command = "nice -n 19 ionice -c3 mkvextract tracks " + source_mkv + " " + str (track_count) +":/tmp/Track_" + str (track_count) + "." + extension
  67.       print extract_command
  68.       os.popen ( extract_command )
  69.       # Convert DTS / DTS-HD stream to 6 channel AC3
  70.       convert_command = "nice -n 19 ionice -c3 ffmpeg -i " + "/tmp/Track_" + str (track_count) + "." + extension + " -acodec ac3 -ac 6 -ab 448k /tmp/Track_" + str (track_count) + ".ac3"  
  71.       print convert_command
  72.       os.popen ( convert_command )
  73.  
  74.       # Remember generated AC3 stream for later remuxing
  75.       ac3_tracks.append ("/tmp/Track_" + str (track_count) + ".ac3")
  76.  
  77.       # Clean up converted DTS / DTS-HD stream
  78.       os.remove ("/tmp/Track_" + str (track_count) + "." + extension)
  79.  
  80.     # Increase track count
  81.     track_count = track_count + 1
  82.    
  83. cl = ""
  84.  
  85. # Generate list of all available AC3 files for remux
  86. for ac3_track in ac3_tracks:
  87.   cl = cl + " " + ac3_track      
  88.  
  89. # Merge generated AC3 streams with old MKV (without any audio) to new MKV
  90. command = "mkvmerge -o " + target_mkv + " --no-audio " + source_mkv + " " + cl
  91. print command
  92. os.popen (command)
  93.  
  94. # Clean up temporary generated AC3 tracks
  95. for ac3_track in ac3_tracks:
  96.   os.remove (ac3_track)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement