def extract_image(source, destination, time=0.2): log.debug('extract_image - %s', os.path.basename(source)) cmds = ( lambda: _run_tool( *ENCODE_VIDEO_COMMON_ARGS, '-i', source, *cmd_args( ss=str(time), vframes=1, an=None, vf=CONFIG['encode_preview']['vf'], ), destination, ), lambda: (os.path.exists(destination), 'expected destiantion image file was not generated (video source may be damaged) {0}'.format(source)), lambda: _run_tool( 'jpegoptim', #'--size={}'.format(CONFIG['jpegoptim']['target_size_k']), '--strip-all', '--overwrite', destination, ), ) for cmd in cmds: cmd_success, cmd_result = cmd() if not cmd_success: return False, cmd_result return True, None
def encode_audio(source, destination, **kwargs): """ Decompress audio Normalize audio volume Offset audio """ log.debug('encode_audio - %s', os.path.basename(source)) path = os.path.dirname(destination) cmds = ( lambda: _run_tool( *ENCODE_VIDEO_COMMON_ARGS, '-i', source, *cmd_args( **CONFIG['extract_audio'] ), os.path.join(path, 'audio_raw.wav'), ), lambda: _run_tool( 'sox', os.path.join(path, 'audio_raw.wav'), #os.path.join(path, 'audio_norm.wav'), destination, 'fade', 'l', '0.15', '0', '0.15', 'norm', ), # TODO: Cut and offset ) for cmd in cmds: cmd_success, cmd_result = cmd() if not cmd_success: return False, cmd_result return True, None
def _run_tool(*args, **kwargs): cmd = cmd_args(*args, **kwargs) log.debug(cmd) cmd_result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=CONFIG['process_timeout_seconds']) if cmd_result.returncode != 0: log.error(cmd_result) return CommandResult(cmd_result.returncode == 0, cmd_result)
def encode_image_to_video(source, destination, duration=10, width=320, height=240, **kwargs): """ Todo: use same codec as encode_video """ log.debug('encode_image_to_video - %s', os.path.basename(source)) _run_tool( *ENCODE_VIDEO_COMMON_ARGS, '-loop', '1', '-i', source, *cmd_args( t=duration, **CONFIG['encode_image_to_video'], ), destination, )
def encode_preview_video(source, destination): """ https://trac.ffmpeg.org/wiki/Encode/AAC#HE-AACversion2 """ log.debug('encode_preview_video - %s', os.path.basename(source)) #crf = crf_from_res(**probe_media(source)) #log.debug('crf: %s', crf) return _run_tool( *ENCODE_VIDEO_COMMON_ARGS, '-i', source, *cmd_args( **CONFIG['encode_preview'] ), destination, )
def encode_video(video_source, audio_source, subtitle_source, destination): log.debug('encode_video - %s', os.path.basename(video_source)) filters = [CONFIG['scale_even']] if subtitle_source: filters.append('subtitles={}'.format(subtitle_source)) return _run_tool( *ENCODE_VIDEO_COMMON_ARGS, '-i', video_source, '-i', audio_source, *cmd_args( vf=', '.join(filters), **CONFIG['encode_video'], ), destination, )
#ac=1, }, # The width and height must be divisable by 2 # Using w=320:h-1 should auto set the height preserving aspect ratio # Sometimes using this method the height is an odd number, which fails to encode # From the avconv documentation we have some mathmatical functions and variables # 'a' is the aspect ratio. floor() rounds down to nearest integer # If we divide by 2 and ensure that an integer, multiplying by 2 must be divisable by 2 }) ENCODE_VIDEO_COMMON_ARGS = cmd_args( 'nice', 'ffmpeg', #threads=CONFIG['threads'], loglevel=CONFIG['log_level'], y=None, ) def check_tools(): """ Assert exteranl dependeycs """ # check ffmpeg (with dependencys) # check sox # check jpegoptim # check (h264 or libx264)? pass
#'profile:a': 'aac_he_v1', #ac=1, }, # The width and height must be divisable by 2 # Using w=320:h-1 should auto set the height preserving aspect ratio # Sometimes using this method the height is an odd number, which fails to encode # From the avconv documentation we have some mathmatical functions and variables # 'a' is the aspect ratio. floor() rounds down to nearest integer # If we divide by 2 and ensure that an integer, multiplying by 2 must be divisable by 2 }) ENCODE_VIDEO_COMMON_ARGS = cmd_args( 'ffmpeg', threads=CONFIG['threads'], loglevel=CONFIG['log_level'], y=None, ) def check_tools(): """ Assert exteranl dependeycs """ # check (h264 or libx264): # avprobe # check ffmpeg (with dependencys) # check sox pass