Guest User

MKV DTS to AC3 Converter - Strips DTS streams

a guest
Feb 19th, 2013
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 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. # Import module OS
  6. import os, sys
  7.  
  8. param_count = len(sys.argv)
  9.  
  10. # Print commandline if not enough parameters are given
  11. if param_count < 2:
  12.   print "mkvdts@ac3 - The DTS / DTS-HD to AC3 converter"
  13.   print
  14.   print "Usage: mkvdts@ac3 /path/to/my.movie.mkv  /path/to/output/"
  15.   quit()
  16.  
  17. # Get source MKV from MKV
  18. source_mkv = sys.argv[1]
  19.  
  20. # Get target directory from CLI
  21. target_dir = sys.argv[2]
  22.  
  23. # Get track information of source MKV
  24. tracks = os.popen ("mkvmerge -i " + source_mkv )
  25.  
  26. # Check for existence of DTS streams
  27. check_DTS = os.popen ("mkvmerge -i " + source_mkv + " \| grep DTS")
  28.  
  29. print check_DTS
  30.  
  31. quit()
  32.  
  33. track_count = 1
  34.  
  35. ac3_tracks = []
  36.  
  37. # Loop through all tracks
  38. for track in tracks:
  39.   # Only process strings with "Track ID" in them
  40.   if track.find ('Track ID') <> -1:
  41.     # Remove whitespaces
  42.     track = track.strip()
  43.    
  44.     if track.find ('A_DTS') <> -1:
  45.       extension = 'dts'
  46.       # Extract DTS / DTS-HD stream from MKV
  47.       extract_command = "nice -n 19 ionice -c3 mkvextract tracks " + source_mkv + " " + str (track_count) +":/tmp/Track_" + str (track_count) + "." + extension
  48.       print extract_command
  49.       os.popen ( extract_command )
  50.       # Convert DTS / DTS-HD stream to 6 channel AC3
  51.       convert_command = "nice -n 19 ionice -c3 ffmpeg -i " + "/tmp/Track_" + str (track_count) + "." + extension + " -acodec ac3 -ac 6 -ab 448k -y /tmp/Track_" + str (track_count) + ".ac3"  
  52.       print convert_command
  53.       os.popen ( convert_command )
  54.  
  55.       # Remember generated AC3 stream for later remuxing
  56.       ac3_tracks.append ("/tmp/Track_" + str (track_count) + ".ac3")
  57.  
  58.       # Clean up converted DTS / DTS-HD stream
  59.       os.remove ("/tmp/Track_" + str (track_count) + "." + extension)
  60.  
  61.     # Increase track count
  62.     track_count = track_count + 1
  63.    
  64. cl = ""
  65.  
  66. # Generate list of all available AC3 files for remux
  67. for ac3_track in ac3_tracks:
  68.   cl = cl + " " + ac3_track      
  69.    
  70. # Target MKV = source MKV filename plus output path
  71. target_mkv = target_dir + os.path.basename(source_mkv)      
  72.  
  73. # Merge generated AC3 streams with old MKV to new MKV
  74. command = "mkvmerge -o " + target_mkv + " " + source_mkv + " " + cl
  75. print command
  76. os.popen (command)
  77.  
  78. # Clean up temporary generated AC3 tracks
  79. for ac3_track in ac3_tracks:
  80.   os.remove (ac3_track)
Add Comment
Please, Sign In to add comment