Example #1
0
    def crop(self, width, height, top, left, out_file, **kwargs):
        ''' Applies crop video filter for width x height pixels '''

        if width is str:
            width = int(width)
        if height is str:
            height = int(height)
        i_bitrate = self.get_video_bitrate()
        i_w, i_h = self.get_dimensions()
        media_opts = self.get_properties()
        media_opts[opt.media.ACODEC] = 'copy'
        # Target bitrate proportional to crop level (+ 20%)
        media_opts[opt.media.VBITRATE] = int(
            int(i_bitrate) * (width * height) / (int(i_w) * int(i_h)) * 1.2)

        media_opts.update(util.cleanup_options(kwargs))
        util.logger.info("Cmd line settings = %s", str(media_opts))
        out_file = util.automatic_output_file_name(out_file, self.filename, \
            "crop_{0}x{1}-{2}x{3}".format(width, height, top, left))
        aspect = __get_aspect_ratio__(width, height, **kwargs)


        cmd = '-i "%s" %s %s -aspect %s "%s"' % (self.filename, \
            media.build_ffmpeg_options(media_opts), media.get_crop_filter_options(width, height, top, left), \
            aspect, out_file)
        util.run_ffmpeg(cmd)
        return out_file
Example #2
0
    def deshake(self, width, height, out_file, **kwargs):
        ''' Applies deshake video filter for width x height pixels '''
        media_opts = self.get_properties()
        media_opts.update({
            opt.media.DEINTERLACE: None,
            opt.media.ASPECT: self.get_aspect_ratio()
        })
        media_opts.update(util.cleanup_options(kwargs))

        if out_file is None or 'nocrop' in kwargs:
            output_file = util.add_postfix(self.filename,
                                           "deshake_%dx%d" % (width, height))
        else:
            output_file = out_file

        ffopts = opt.media2ffmpeg(media_opts)
        cmd = '-i "%s" %s %s "%s"' % (self.filename, \
            util.dict2str(ffopts), get_deshake_filter_options(width, height), output_file)
        util.run_ffmpeg(cmd)

        if 'nocrop' not in kwargs:
            return output_file

        new_w = self.get_width() - width
        new_h = self.get_height() - height
        if out_file is None:
            output_file2 = util.add_postfix(
                self.filename, "deshake_crop_%dx%d" % (new_w, new_h))
        else:
            output_file2 = out_file
        deshake_file_o = VideoFile(output_file)
        kwargs.update({opt.media.ASPECT: self.get_aspect_ratio()})
        deshake_file_o.crop(new_w, new_h, width // 2, height // 2,
                            output_file2, **kwargs)
        os.remove(output_file)
        return output_file2
Example #3
0
            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)


parser = util.parse_common_args('Audio and Video file (re)encoder')
parser = video.add_video_args(parser)

myargs = parser.parse_args()
myoptions = vars(myargs)
util.check_environment(myoptions)
myoptions = util.cleanup_options(myoptions)

if os.path.isdir(myargs.inputfile):
    encode_dir(myargs, myoptions)
else:
    encode_file(myargs, myoptions)
Example #4
0
import re
import argparse
import mediatools.utilities as util
import mediatools.videofile as video


def parse_args():
    parser = util.parse_common_args(desc='Apply deshake filter')
    parser = video.add_video_args(parser)
    parser.add_argument('--width', required=True, help='Deshake width')
    parser.add_argument('--height', required=True, help='Deshake height')
    parser.add_argument('--nocrop',
                        required=False,
                        help='Do not crop video after deshaking')
    return parser.parse_args()


args = parse_args()
kwargs = vars(args)
util.check_environment(kwargs)
kwargs = util.cleanup_options(kwargs)
del kwargs['width']
del kwargs['height']
if args.timeranges is not None:
    for video_range in re.split(',', args.timeranges):
        kwargs['ss'], kwargs['to'] = re.split('-', video_range)
outputfile = video.deshake(args.inputfile, int(args.width), int(args.height),
                           args.outputfile, **kwargs)
util.debug(1, 'Generated %s' % outputfile)
Example #5
0
                    required=False,
                    default='',
                    help='Types of files to include [audio,video,image]')
parser.add_argument('-g',
                    '--debug',
                    required=False,
                    default=0,
                    help='Debug level')
parser.add_argument('--dry_run',
                    required=False,
                    default=0,
                    help='Dry run mode')
args = parser.parse_args()
options = vars(args)
util.check_environment(options)
util.cleanup_options(options)

filelist = []
if os.path.isdir(args.inputfile):
    if args.types == '':
        types = ['video', 'audio', 'image']
    else:
        types = re.split(',', args.types.lower())
    if 'video' in types:
        filelist.extend(util.video_filelist(args.inputfile))
    if 'audio' in types:
        filelist.extend(util.audio_filelist(args.inputfile))
    if 'image' in types:
        filelist.extend(util.image_filelist(args.inputfile))
else:
    filelist = [args.inputfile]