def encode_dir(args, options):
    '''Encodes a whole directory'''
    targetdir = args.inputfile + '.' + args.profile
    util.logger.debug("%s ==> %s", args.inputfile, targetdir)
    os.makedirs(targetdir, exist_ok=True)

    ext = util.get_profile_extension(args.profile)
    filelist = util.filelist(args.inputfile)
    nbfiles = len(filelist)
    i = 0
    for fname in filelist:
        util.logger.info("%5d/%5d : %3d%% : %s", i, nbfiles,
                         round(i * 100 / nbfiles), fname)
        targetfname = fname.replace(args.inputfile, targetdir, 1)
        if util.is_audio_file(fname) or util.is_video_file(fname):
            targetfname = util.strip_file_extension(targetfname) + r'.' + ext
            directory = os.path.dirname(targetfname)
            if not os.path.exists(directory):
                os.makedirs(directory)
            if util.is_audio_file(fname):
                o_file = audio.AudioFile(fname)
            else:
                o_file = video.VideoFile(fname)
            outputfile = o_file.encode(targetfname, args.profile, **options)
            util.logger.info("File %s generated", outputfile)
        else:
            # Simply copy non media files
            copyfile(fname, targetfname)
            util.logger.info("Skipping/Plain Copy file %s", fname)
        i = i + 1
    util.logger.info('%05d/%05d : 100%% : Job finished', nbfiles, nbfiles)
def filelist_album_art(filelist, image_file):
    for file in filelist:
        if util.is_audio_file(file):
            util.logger.info('Encoding album art %s in file %s', image_file,
                             file)
            audio.encode_album_art(file, image_file,
                                   **{'scale': DEFAULT_RESCALING})
def encode_file(args, options):
    '''Encodes a single file'''
    if util.is_audio_file(args.inputfile):
        file_object = audio.AudioFile(args.inputfile)
    elif util.is_image_file(args.inputfile):
        file_object = img.ImageFile(args.inputfile)
    else:
        file_object = video.VideoFile(args.inputfile)
    if args.width is not None:
        specs = file_object.get_properties()
        w = int(specs[opt.media.WIDTH])
        h = int(specs[opt.media.HEIGHT])
        new_w = int(args.width)
        new_h = (int(h * new_w / w) // 8) * 8
        options[opt.media.SIZE] = "%dx%d" % (new_w, new_h)
    if args.timeranges is None:
        file_object.encode(args.outputfile, args.profile, **options)
        return

    if args.outputfile is None:
        ext = util.get_profile_extension(args.profile)
    count = 0
    filelist = []
    timeranges = re.split(',', args.timeranges)
    for video_range in timeranges:
        options[opt.media.START], options[opt.media.STOP] = re.split(
            '-', video_range)
        count += 1
        target_file = util.automatic_output_file_name(args.outputfile,
                                                      args.inputfile,
                                                      str(count), ext)
        filelist.append(target_file)
        outputfile = file_object.encode(target_file, args.profile, **options)
        util.logger.info("File %s generated", outputfile)
    if len(timeranges) > 1:
        # If more than 1 file generated, concatenate all generated files
        target_file = util.automatic_output_file_name(args.outputfile,
                                                      args.inputfile,
                                                      "combined", ext)
        video.concat(target_file, filelist)
Exemple #4
0
#!/usr/local/bin/python3

# This script multiplexes an extra audio track
# in a video file with already one audio track

import sys
import os
import mediatools.videofile as video
import mediatools.utilities as util

util.set_debug_level(5)
afiles = []
for i in range(0, len(sys.argv)):
    file = sys.argv[i]
    if util.is_video_file(file):
        vfile = file
        util.logger.info("Video file %s will be muxed", file)
    elif util.is_audio_file(file):
        util.logger.info("Audio file %s will be muxed", file)
        afiles.append(file)

videofile = video.VideoFile(vfile)
videofile.add_audio_tracks(*afiles)