Changeset 19f68114ed66d3e613ac582097a372e399f4af17


Ignore:
Timestamp:
07/29/11 14:02:28 (10 months ago)
Author:
Luper Rouch <luper.rouch@…>
Children:
7cebbdb62a64026be49de41d00e78e6af5cedef2, 3a775b411f35b86eb3571e5ee1847dbfa4067f9c
Parents:
70076a735ad14f19763a31ed1f6e806027043cba
git-committer:
Luper Rouch <luper.rouch@…> (07/29/11 14:02:28)
Message:

avi2mkv: cleaned up mkvmerge options, should support all arangements of tracks now

File:
1 edited

Legend:

Unmodified
Added
Removed
  • utils/avi2mkv/src/avi2mkv/__init__.py

    r70076a7 r19f6811  
    11import os 
    22import os.path as op 
     3import re 
    34import subprocess 
    45import argparse 
     
    910 
    1011 
    11 def mux_avi(args): 
     12def mux_file(args): 
    1213    """ 
    1314    Mux an avi and its subtitles (if any) in a mkv file. 
     
    3637    else: 
    3738        subtitles_file = None 
    38     # Run mkvmerge 
    39     args = [ 
    40         "mkvmerge",  
    41         "-o", output_file,  
    42         "--quiet", 
    43         "--forced-track", "0:no",  
    44         "--compression", "0:none",  
    45         "--forced-track", "1:no", 
    46         "--compression", "1:none",  
    47         "-a", "1",  
    48         "-d", "0",  
    49         "-S",  
    50         "-T", 
    51         "--no-global-tags",  
    52         "--no-chapters", 
    53         input_file 
    54     ] 
     39    # Build mkvmerge args 
     40    args = ["mkvmerge", "-o", output_file] 
     41    for track in list_tracks(input_file): 
     42        args += ["--compression", "%d:none" % track] 
    5543    if subtitles_file is not None: 
     44        args += ["-S", input_file] 
    5645        out_fd = tempfile.NamedTemporaryFile() 
    5746        subrip.clean_srt(open(subtitles_file, "rt"), out_fd) 
     
    5948        args += [ 
    6049            "--sub-charset", "0:%s" % options.charset,  
    61             "--forced-track", "0:no",  
    6250            "--compression", "0:none",  
    63             "-s", "0", 
    64             "-D",  
    65             "-A",  
    66             "-T",  
    67             "--no-global-tags",  
    68             "--no-chapters", 
    6951            out_fd.name,  
    70             "--track-order", "0:0,0:1,1:0" 
    7152        ] 
    7253    else: 
    73         args += ["--track-order", "0:0,0:1"] 
     54        args += [input_file] 
    7455    kwargs = {} 
    7556    if not options.quiet: 
     
    7758                (input_file, output_file)) 
    7859    else: 
     60        args += ["--quiet"] 
    7961        fnull = open(os.devnull, "w") 
    8062        kwargs["stdout"] = fnull 
    8163        kwargs["stderr"] = fnull 
     64    # Run mkvmerge 
     65    if options.verbose: 
     66        sys.stdout.write("mkvmerge command line:\n%s\n" % " ".join(args)) 
    8267    try: 
    8368        subprocess.check_call(args, **kwargs) 
     
    8570        sys.stderr.write("Error muxing '%s', mkvmerge exited with error " 
    8671                "code %s\n" % (input_file, err.returncode)) 
     72 
     73 
     74def list_tracks(filename): 
     75    """ 
     76    List and return track IDs in *filename* with ``mkvmerge --identify``. 
     77    """ 
     78    process = subprocess.Popen(["mkvmerge", "--identify", filename], 
     79            stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
     80    stdout, stderr = process.communicate() 
     81    track_pattern = re.compile("Track ID ([0-9]+):") 
     82    ids = [] 
     83    for line in stdout.splitlines(): 
     84        match = track_pattern.match(line) 
     85        if match: 
     86            ids.append(int(match.group(1))) 
     87    return ids 
    8788 
    8889 
     
    100101            help="output directory (default is to create the new files in the " 
    101102            "same directory as the input files)") 
     103    parser.add_argument("--force", "-f", action="store_true", default=False, 
     104            help="force overwrite of existing files") 
    102105    parser.add_argument("--quiet", "-q", action="store_true", default=False,  
    103106            help="be quiet") 
    104     parser.add_argument("--force", "-f", action="store_true", default=False, 
    105             help="force overwrite of existing files") 
     107    parser.add_argument("--verbose", action="store_true", default=False,  
     108            help="be verbose") 
    106109    args = parser.parse_args() 
    107110    # Run jobs 
    108111    pool = multiprocessing.Pool(args.num_jobs) 
    109     pool.map(mux_avi, [(f, args) for f in args.input_files]) 
     112    pool.map(mux_file, [(f, args) for f in args.input_files]) 
Note: See TracChangeset for help on using the changeset viewer.