コード例 #1
0
def test_concat_video():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=True)
    h264_obj1 = H264Video(constval.VIDEO1, constval.OUTPUT_DIR, aio=True)
    print('current work dir', os.path.abspath(os.getcwd()))
    home_dir = os.path.abspath(os.getenv('HOME'))
    concat_video, stderr = h264_obj.cmd_do(f'{home_dir:s}',
                                           'mp4',
                                           FfmpegCmdModel.concat_video,
                                           input_obj=h264_obj1,
                                           encode_lib=constval.CODEC)
    # print(stderr)
    assert concat_video is not None and stderr == ''
    print('H264Video object info:', concat_video)
    print(
        f'out put video width:{concat_video.video_width:d},video height:{concat_video.video_height:d},'
        f'video bit rate:{concat_video.video_bitrate:d}')
    add_test1 = h264_obj + h264_obj1
    add_test2 = h264_obj1 + h264_obj
    print('add_test1 H264Video object info:', add_test1)
    print(
        f'add_test1 out put video width:{add_test1.video_width:d},video height:{add_test1.video_height:d},'
        f'video bit rate:{add_test1.video_bitrate:d}')
    print('add_test2 H264Video object info:', add_test2)
    print(
        f'add_test2 out put video width:{add_test2.video_width:d},video height:{add_test2.video_height:d},'
        f'video bit rate:{add_test2.video_bitrate:d}')
コード例 #2
0
def test_snapshot_video():
    """
    测试视频缩放
    :return:
    """
    print('')
    assert all(
        (os.path.isfile(constval.VIDEO), os.path.isfile(constval.FFMPEG_BIN),
         os.path.isfile(constval.FFPROBE_BIN)))
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR,
                         constval.FFMPEG_BIN, constval.FFPROBE_BIN, False)
    assert not hasattr(h264_obj, 'cmd_do_aio')
    home_dir = os.getenv('HOME')
    start_time = random.random() * 100
    target_height = random.randint(120, 480)
    print(
        f'out put jpg start time:{start_time:f},jpg height:{target_height:d}')
    print('current work dir', os.path.abspath(os.getcwd()))
    jpgpath, stderr = h264_obj.cmd_do(f'{home_dir:s}',
                                      'jpg',
                                      FfmpegCmdModel.snapshot_video,
                                      start_time=start_time,
                                      target_height=target_height)

    assert jpgpath is not None and stderr == ''
    print('jpg path:', jpgpath)
    # 下面测试视频批量截图
    slice_begin = random.randint(0, 120)
    slice_end = random.randint(slice_begin, 240)
    slice_count = random.randint(1, 20)
    print(
        f'begin: {slice_begin:d}, end: {slice_end:d}, count: {slice_count:d}')
    print(h264_obj[slice_begin:slice_end:-slice_count])
コード例 #3
0
ファイル: test_init_del.py プロジェクト: ucrux/aioffmpeg
def test_init_del():
    """
    测试初始化
    验证 视频文件 在资源释放是是否会被删除
    :param tmpdir: 临时目录, 由 pytest 管理
    :return:
    """
    assert all(
        (os.path.isfile(constval.VIDEO), os.path.isfile(constval.FFMPEG_BIN),
         os.path.isfile(constval.FFPROBE_BIN),
         os.path.isfile(constval.VIDEO_DUMMY),
         os.path.isfile(constval.FFMPEG_BIN_DUMMY),
         os.path.isfile(constval.FFPROBE_BIN_DUMMY)))
    h264video_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=True)
    assert any((h264video_obj.libx264, h264video_obj.h264_nvenc))
    attr_list = [
        'libx264', 'h264_nvenc', 'output_dir', 'video_codecname',
        'video_profile', 'video_width', 'video_height', 'video_pixfmt',
        'video_avgframerate', 'video_bitrate', 'audio_codecname',
        'audio_profile', 'audio_samplefmt', 'audio_samplerate',
        'audio_channels', 'audio_bitrate', 'videofile_path',
        'videofile_duration', 'videofile_size', 'videofile_formatname'
    ]
    assert all([hasattr(h264video_obj, attr) for attr in attr_list])
    del h264video_obj
コード例 #4
0
async def test_rotate_video_aio():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR,
                         constval.FFMPEG_BIN, constval.FFPROBE_BIN, True)
    print('current work dir', os.path.abspath(os.getcwd()))
    home_dir = os.path.abspath(os.getenv('HOME'))
    rotate_direct = H264EncoderArgs.v_left_rotate if random.randint(
        0, 1) == 0 else H264EncoderArgs.v_right_rotate
    print(f'rotate_drect: {rotate_direct:d}')
    scaled_obj, stderr = await h264_obj.cmd_do_aio(
        f'{home_dir:}',
        'mp4',
        FfmpegCmdModel.rotate_video,
        rotate_direct=rotate_direct,
        target_videobitrate=random.randint(100, 400),
        encode_lib=constval.CODEC)
    assert scaled_obj is not None and stderr == ''
    print('H264Video object info:', scaled_obj)
    print(
        f'out put video width:{scaled_obj.video_width:d},video height:{scaled_obj.video_height:d},'
        f'video bit rate:{scaled_obj.video_bitrate:d}')
コード例 #5
0
def test_rotate_video():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=False)
    assert not hasattr(h264_obj, 'cmd_do_aio')
    print('current work dir', os.path.abspath(os.getcwd()))
    home_dir = os.path.abspath(os.getenv('HOME'))
    rotate_direct = H264EncoderArgs.v_left_rotate if random.randint(
        0, 1) == 0 else H264EncoderArgs.v_right_rotate
    print(f'rotate_drect: {rotate_direct:d}')
    scaled_obj, stderr = h264_obj.cmd_do(
        f'{home_dir:}',
        'mp4',
        FfmpegCmdModel.rotate_video,
        rotate_direct=rotate_direct,
        target_videobitrate=random.randint(100, 400),
        hwaccel=H264EncoderArgs.hwaccel_cuda,
        decoder=H264EncoderArgs.decoder_h264_cuvid,
        encode_lib=H264EncoderArgs.codec_v_libx264)
    assert scaled_obj is not None and stderr == ''
    print('H264Video object info:', scaled_obj)
    print(
        f'out put video width:{scaled_obj.video_width:d},video height:{scaled_obj.video_height:d},'
        f'video bit rate:{scaled_obj.video_bitrate:d}')
コード例 #6
0
ファイル: cmdline_tool.py プロジェクト: ucrux/aioffmpeg
def _init_tools(input_file:str = None, output:str = None) -> tuple:
    """
    使用在main中的工具函数
    :param: input_file: 如果不为None,则计算绝对路径,且初始化成H264Video实例
    :param: output: 如果不为None,则计算绝对路径
    return  H264Video obj(None or Not None), abspath output(None or not None)
    """
    status, stdout, _ = simple_run_cmd(r'which ffmpeg')
    if status != 0:
        print(r'can not find ffmpeg bin in $PATH')
        exit(-1)
    ffmpeg_bin = stdout.replace('\n', '')
    status, stdout, stderr = simple_run_cmd(r'which ffprobe')
    if status != 0:
        print(r'can not find ffprobe bin in $PATH')
        exit(-1)
    ffprobe_bin = stdout.replace('\n', '')
    if input_file is not None:
        input_file = os.path.abspath(input_file)
        try: 
            h264_obj = H264Video(input_file, r'/tmp', ffmpeg_bin, ffprobe_bin, aio=False)
        except (Exception,FileNotFoundError):
            print(f'initial H264Video failed with {ffmpeg_bin} {ffprobe_bin} {input_file}')
            exit(-1)
    else:
        h264_obj = None
    if output is not None:
        output = os.path.abspath(output)
    return h264_obj, output
コード例 #7
0
def test_scale_video_cuda():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=False)
    assert not hasattr(h264_obj, 'cmd_do_aio')
    print('current work dir', os.path.abspath(os.getcwd()))
    home_dir = os.path.abspath(os.getenv('HOME'))
    scaled_obj, stderr = h264_obj.cmd_do(
        f'{home_dir:s}',
        'mp4',
        FfmpegCmdModel.scale_video,
        target_width=random.randint(700, 1000),
        target_height=random.randint(300, 1000),
        target_videobitrate=random.randint(100, 400),
        encode_lib=H264EncoderArgs.codec_v_h264_nvenc,
        #encode_lib=H264EncoderArgs.codec_v_libx264,
        hwaccel=H264EncoderArgs.hwaccel_cuvid,
        #decoder=H264EncoderArgs.decoder_h264_cuvid,
        ac=H264EncoderArgs.audio_channel_1,
        ar=H264EncoderArgs.audio_simple_rate_48000)
    assert scaled_obj is not None and stderr == ''
    print('H264Video object info:', scaled_obj)
    print(
        f'out put video width:{scaled_obj.video_width:d},video height:{scaled_obj.video_height:d},'
        f'video bit rate:{scaled_obj.video_bitrate:d}')
コード例 #8
0
ファイル: test_cut_video.py プロジェクト: ucrux/aioffmpeg
def test_cut_video():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=False)
    start_time = random.random() * 100
    last_time = random.randint(int(start_time) + 1, 1000)
    assert not hasattr(h264_obj, 'cmd_do_aio')
    print('current work dir', os.path.abspath(os.getcwd()))
    print(f'start_time: {start_time:f}, last_time: {last_time:d}')
    home_dir = os.path.abspath(os.getenv('HOME'))
    cuted_video, stderr = h264_obj.cmd_do(f'{home_dir:s}',
                                          'mp4',
                                          FfmpegCmdModel.cut_video,
                                          start_time=start_time,
                                          last_time=last_time,
                                          encode_lib=constval.CODEC,
                                          target_videobitrate=500)

    assert cuted_video is not None and stderr == ''
    print('H264Video object info:', cuted_video)
    print(
        f'out put video width:{cuted_video.video_width:d},video height:{cuted_video.video_height:d},'
        f'video bit rate:{cuted_video.video_bitrate:d}')
    slice_begin = random.randint(0, 120)
    slice_end = random.randint(slice_begin, 240)
    slice_count = random.randint(0, 20)
    print(
        f'begin: {slice_begin:d}, end: {slice_end:d}, count: {slice_count:d}')
    print(h264_obj[slice_begin:slice_end:slice_count])
    print(h264_obj[slice_end])
コード例 #9
0
async def test_scale_video_cuda_aio():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=True)
    print('current work dir', os.path.abspath(os.getcwd()))
    home_dir = os.path.abspath(os.getenv('HOME'))
    scaled_obj, stderr = await h264_obj.cmd_do_aio(
        f'{home_dir:}',
        'mp4',
        FfmpegCmdModel.scale_video,
        target_width=random.randint(700, 1000),
        target_height=random.randint(300, 1000),
        target_videobitrate=random.randint(100, 400),
        encode_lib=H264EncoderArgs.codec_v_h264_qsv,
        #encode_lib=H264EncoderArgs.codec_v_libx264,
        hwaccel=H264EncoderArgs.hwaccel_cuda,
        decoder=H264EncoderArgs.decoder_h264_cuvid)
    assert scaled_obj is not None and stderr == ''
    print('H264Video object info:', scaled_obj)
    print(
        f'out put video width:{scaled_obj.video_width:d},video height:{scaled_obj.video_height:d},'
        f'video bit rate:{scaled_obj.video_bitrate:d}')
コード例 #10
0
ファイル: test_logo_video.py プロジェクト: ucrux/aioffmpeg
async def test_logo_video_aio():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=True)
    print('current work dir', os.path.abspath(os.getcwd()))
    home_dir = os.path.abspath(os.getenv('HOME'))
    ratio_img_height = 0.981555
    img_position_x = 412
    img_position_y = 1662
    #ratio_img_height = random.random()
    #img_position_x = random.randint(0, 1000)
    #img_position_y = random.randint(0, 2000)
    print(
        f'ratio_img_height:{ratio_img_height:f}, '
        f'img_position_x={img_position_x:d}, img_position_y={img_position_y:d}'
    )
    # 固定水印
    fix_video_logo, stderr = await h264_obj.cmd_do_aio(
        f'{home_dir:s}',
        'mp4',
        FfmpegCmdModel.logo_video_fix,
        input_img=constval.LOGO,
        ratio_img_height=ratio_img_height,
        img_position_x=img_position_x,
        img_position_y=img_position_y,
        encode_lib=constval.CODEC)
    assert fix_video_logo is not None and stderr == ''
    print('H264Video object info:', fix_video_logo)
    print(
        f'out put video width:{fix_video_logo.video_width:d},video height:{fix_video_logo.video_height:d},'
        f'video bit rate:{fix_video_logo.video_bitrate:d}')
    # 移动水印
    fix_video_move, stderr = await h264_obj.cmd_do_aio(
        f'{home_dir:s}',
        'mp4',
        FfmpegCmdModel.logo_video_move,
        input_img=constval.LOGO,
        ratio_img_height=ratio_img_height,
        img_position_x=img_position_x,
        img_position_y=img_position_y,
        encode_lib=constval.CODEC)
    assert fix_video_move is not None and stderr == ''
    print('H264Video object info:', fix_video_move)
    print(
        f'out put video width:{fix_video_move.video_width:d},video height:{fix_video_move.video_height:d},'
        f'video bit rate:{fix_video_move.video_bitrate:d}')
コード例 #11
0
def test_delog_video():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=True)
    print('current work dir', os.path.abspath(os.getcwd()))
    home_dir = os.path.abspath(os.getenv('HOME'))
    delog_args = tuple([
                        H264Video.create_delog_args(random.randint(0, 600),
                                                    random.randint(0, 1000),
                                                    random.randint(0, 300),
                                                    random.randint(0, 300),
                                                    random.randint(0, 100),
                                                    random.randint(100, 200))
        for i in range(10)])
    delog_obj, stderr = h264_obj.cmd_do(f'{home_dir:}', 'mp4', FfmpegCmdModel.del_log,
                                        delog_tuple=delog_args,
                                        encode_lib=constval.CODEC)
    assert delog_obj is not None and stderr == ''
    print('H264Video object info:', delog_obj)
    print(f'out put video width:{delog_obj.video_width:d},video height:{delog_obj.video_height:d},'
          f'video bit rate:{delog_obj.video_bitrate:d}')
コード例 #12
0
def test_scale_video_qsv():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=False)
    assert not hasattr(h264_obj, 'cmd_do_aio')
    print('current work dir', os.path.abspath(os.getcwd()))
    home_dir = os.path.abspath(os.getenv('HOME'))
    scaled_obj, stderr = h264_obj.cmd_do(f'{home_dir:s}', 'mp4', FfmpegCmdModel.scale_video_qsv,
                                         target_width=random.randint(700, 1000),
                                         target_height=random.randint(300, 1000),
                                         target_videobitrate=random.randint(100, 400))
    assert scaled_obj is not None and stderr == ''
    print('H264Video object info:', scaled_obj)
    print(f'out put video width:{scaled_obj.video_width:d},video height:{scaled_obj.video_height:d},'
          f'video bit rate:{scaled_obj.video_bitrate:d}')
コード例 #13
0
def test_hls_video():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=False)
    home_dir = os.getenv('HOME')
    assert not hasattr(h264_obj, 'cmd_do_aio')
    video_bitrate = random.randint(100, 400)
    ts_time = random.randint(5, 15)
    print('current work dir', os.path.abspath(os.getcwd()))
    m3u8path, stderr = h264_obj.cmd_do(f'{home_dir:s}',
                                       'm3u8',
                                       FfmpegCmdModel.hls_video,
                                       target_videobitrate=video_bitrate,
                                       encode_lib=constval.CODEC,
                                       ts_time=ts_time,
                                       ts_prefix='test-ts',
                                       hls_enc=1)

    assert m3u8path is not None and stderr == ''
    print('m3u8 path:', m3u8path)
    print(f'out put video bit rate:{video_bitrate:d},ts seg time:{ts_time:d}')
    m3u8path, stderr = h264_obj.cmd_do(f'{home_dir:s}',
                                       'm3u8',
                                       FfmpegCmdModel.hls_video,
                                       target_videobitrate=video_bitrate,
                                       encode_lib=constval.CODEC,
                                       ts_time=ts_time,
                                       ts_prefix='test-ts',
                                       hls_enc=1,
                                       hls_enc_key='0123456789abcdef',
                                       hls_enc_key_url='https://www.baidu.com')

    assert m3u8path is not None and stderr == ''
    print('m3u8 path:', m3u8path)
    print(f'out put video bit rate:{video_bitrate:d},ts seg time:{ts_time:d}')
コード例 #14
0
async def test_create_gif_aio():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=True)
    start_time = random.random() * 100
    last_time = random.randint(int(start_time)+1, int(start_time)+50)
    target_height = random.randint(100, 720)
    print('current work dir', os.path.abspath(os.getcwd()))
    print(f'start_time: {start_time:f}, last_time: {last_time:d}, target_height: {target_height:d}, '
          f'video len: {h264_obj.videofile_duration:f}')
    print(start_time, last_time)
    home_dir = os.path.abspath(os.getenv('HOME'))
    gifpath, stderr = await h264_obj.cmd_do_aio(f'{home_dir:s}', 'gif', FfmpegCmdModel.create_gif,
                                                start_time=start_time,
                                                last_time=last_time,
                                                v_frame=5,
                                                target_height=target_height)

    assert gifpath is not None and stderr == ''
    print('jpg path:', gifpath)
コード例 #15
0
async def test_hls_video_qsv_aio():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=True)
    home_dir = os.getenv('HOME')
    video_bitrate = random.randint(100, 400)
    ts_time = random.randint(5, 15)
    print('current work dir', os.path.abspath(os.getcwd()))
    m3u8path, stderr = await h264_obj.cmd_do_aio(
        f'{home_dir:s}',
        'm3u8',
        FfmpegCmdModel.hls_video_qsv,
        target_videobitrate=video_bitrate,
        target_height=random.randint(100, 300),
        ts_time=ts_time,
        ts_prefix='test-ts-aio')

    assert m3u8path is not None and stderr == ''
    print('m3u8 path:', m3u8path)
    print(f'out put video bit rate:{video_bitrate:d},ts seg time:{ts_time:d}')
コード例 #16
0
ファイル: test_cut_video_qsv.py プロジェクト: ucrux/aioffmpeg
async def test_cut_video_qsv_aio():
    """
    测试视频缩放
    :return:
    """
    print('')
    h264_obj = H264Video(constval.VIDEO, constval.OUTPUT_DIR, aio=True)
    start_time = random.random() * 100
    last_time = random.randint(int(start_time)+1, 1000)
    print('current work dir', os.path.abspath(os.getcwd()))
    print(f'start_time: {start_time:f}, last_time: {last_time:d}')
    print(start_time, last_time)
    home_dir = os.path.abspath(os.getenv('HOME'))
    cuted_video, stderr = await h264_obj.cmd_do_aio(f'{home_dir:s}', 'mp4', FfmpegCmdModel.cut_video_qsv,
                                                    start_time=start_time,
                                                    last_time=last_time,
                                                    target_height=random.randint(100,300),
                                                    target_videobitrate=500)

    assert cuted_video is not None and stderr == ''
    print('H264Video object info:', cuted_video)
    print(f'out put video width:{cuted_video.video_width:d},video height:{cuted_video.video_height:d},'
          f'video bit rate:{cuted_video.video_bitrate:d}')
コード例 #17
0
ファイル: cmdline_tool.py プロジェクト: ucrux/aioffmpeg
def main():
    parser = argparse.ArgumentParser(description='aioffmpeg command line tools')
    parser.add_argument(r'--function', r'-f', required=True, choices='msctndagh', default=None,
                        help=r"this tool's functions, m: concat ts from m3u8 file, s: scale video, "
                             r"c: cut video, t: concat video, n: take a snapshot from video, "
                             r"d: delete video logo, a: add logo to video, "
                             r"g: make a gif from video, h: make hls ts from video")
    parser.add_argument(r'--input', r'-i', required=True, default=None, 
                        action=r'append', nargs=r'+', 
                        help=r'input m3u8 file(local or url), or input video')
    parser.add_argument(r'--output', r'-o', required=True, default=None, help=r'output file(jpg,gif,mp4,m3u8)')
    parser.add_argument(r'--width', r'-w', default=0, help=r"video width, default using source video's width")
    parser.add_argument(r'--height', r'-e', default=0, help=r"video(image) height, default using source video's height,image only has height")
    parser.add_argument(r'--videorate', r'-r', default=0, help=r"video rate, default using source video's video rate")
    parser.add_argument(r'--codeclib', r'-l', choices='gc', default='c', help=r'video codec lib, g: gpu codec, c: cpu codec')
    parser.add_argument(r'--startime', r'-s', default=0, help=r'cut video, make gif or take snapshot begin time')
    parser.add_argument(r'--lastime', r'-t', default=1, help=r'cut vider or make gif last time')
    parser.add_argument(r'--positionx', r'-x', default=None, help=r'delete logo or add logo position x')
    parser.add_argument(r'--positiony', r'-y', default=None, help=r'delete logo or add logo position y')
    parser.add_argument(r'--dwidth', r'-dw',default=1, help=r'delete logo width')
    parser.add_argument(r'--dheight', r'-dh',default=1, help=r'delete logo height')
    parser.add_argument(r'--ratio', r'-rh', default=0.11, help=r'add loge ratio of video height')
    parser.add_argument(r'--giframe', r'-gf', default=5, help=r'gif frame')
    parser.add_argument(r'--tstime', r'-ts', default=10, help=r'ts fragment time')
    args = parser.parse_args()
    # print(args.input)
    try:
        width = int(args.width)
        height = int(args.height)
        videorate = int(args.videorate)
        startime = float(args.startime)
        lastime = float(args.lastime)
        position_x = int(args.positionx) if args.positionx else None
        position_y = int(args.positiony) if args.positiony else None
        dwidth = int(args.dwidth)
        dheight = int(args.dheight)
        ratio = float(args.ratio)
        giframe = int(args.giframe)
        tstime = int(args.tstime)
        assert lastime >= 1
        assert startime >= 0
        assert ratio > 0 and ratio < 1
        if position_x and position_y:
            assert position_x >= 0 and position_y >= 0
        assert dwidth > 0 and dheight > 0
        assert giframe > 0
        assert tstime > 0
    except ValueError:
        print(r'--width, --height, --videorate should be integral')
        print(r'--startime and --lastime must be a number')
        print(r'--positionx and --positiony must be a integral')
        print(r'--dwidth and --dheight must be a integral')
        print(r'--giframe must be a integral')
        print(r'--tstime must be a integral')
        exit(-1)
    except AssertionError:
        print('start time must >= 0, lastime must >= 1')
        print('position_x must >= 0, img_position_y must >= 0')
        print('dwidth must > 0, dheight must > 0')
        print('ratio must > 0, and must < 1')
        print('gif frame must > 0')
        print('ts fragment time must > 0')
        exit(-1)
    encode_lib = H264EncoderArgs.codec_v_h264_nvenc if args.codeclib == 'g' else H264EncoderArgs.codec_v_libx264
    kwargs = dict()
    kwargs['target_width'] = width
    kwargs['target_height'] = height
    kwargs['target_videobitrate'] = videorate
    kwargs['start_time'] = startime
    kwargs['last_time'] = lastime
    kwargs['encode_lib'] = encode_lib
    kwargs['ratio_img_height'] = ratio
    kwargs['ts_time'] = tstime
    kwargs['ts_prefix'] = str(uuid.uuid1())
    try:
        if args.function == r'm':
            status, stdout, _ = simple_run_cmd(r'which ffmpeg')
            if status != 0:
                print(r'can not find ffmpeg bin in $PATH')
                exit(-1)
            ffmpeg_bin = stdout.replace('\n', '')
            status, stdout, stderr = simple_run_cmd(r'which ffprobe')
            if status != 0:
                print(r'can not find ffprobe bin in $PATH')
                exit(-1)
            ffprobe_bin = stdout.replace('\n', '')
            # 合并TS文件
            _, output = _init_tools(None, args.output)
            print(r'begin concat ts ...')
            result, stderr = H264Video.download_m3u8(ffmpeg_bin, args.input[0][0], output)
            if result:
                print(f'concat done, output: {output}')
            else:
                print(f'concat error: {stderr}')
                exit(-1)
        elif args.function == r's':
            # 缩放视频
            h264_obj, output_file = _init_tools(args.input[0][0], args.output)
            print(f'scale video {args.input[0][0]} ...')
            _h264_workflow_base(h264_obj, FfmpegCmdModel.scale_video, 
                                'mp4', output_file=output_file, **kwargs)
            print(f'scale video done, output: {output_file}')
        elif args.function == r'c':
            # 裁减视频
            h264_obj, output_file = _init_tools(args.input[0][0], args.output)
            print(f'cut video {args.input[0][0]} ...')
            _h264_workflow_base(h264_obj, FfmpegCmdModel.cut_video, 'mp4', 
                                output_file=output_file, **kwargs)
            print(f'cut video done, output: {output_file}')
        elif args.function == r't':
            # 拼接视频
            try:
                assert len(args.input) >= 2
            except AssertionError:
                print(f'input file must more than 2')
                exit(-1)
            h264_obj, output_file = _init_tools(args.input[0][0], args.output)
            h264_obj1, _ = _init_tools(args.input[1][0], args.output)
            print(f'concat video {args.input[0][0]} {args.input[1][0]} ...')
            h264out_obj1 = h264_obj + h264_obj1
            status, _, stderr = simple_run_cmd(f'mv -f {h264out_obj1.videofile_path} {output_file}')
            if status != 0:
                print(f'{stderr}')
                exit(-1)
            print(f'concat video done, output: {output_file}')
        elif args.function == r'n':
            startime = int(startime)
            h264_obj, output_file = _init_tools(args.input[0][0], args.output)
            print(f'take snanshot from video {args.input[0][0]} ...')
            _h264_workflow_base(h264_obj, FfmpegCmdModel.snapshot_video, 'jpg', 
                                output_file=output_file, **kwargs)
            print(f'snapshot done, output: {output_file}')
        elif args.function == r'd':
            position_x = 0 if not position_x else position_x
            position_y = 0 if not position_y else position_y
            h264_obj, output_file = _init_tools(args.input[0][0], args.output)
            delog_args = tuple([H264Video.create_delog_args(position_x, position_y, dwidth, dheight,
                                                            int(startime), int(startime + lastime))])
            kwargs['delog_tuple'] = delog_args
            print(f'delete logo from video {args.input[0][0]} ...')
            _h264_workflow_base(h264_obj, FfmpegCmdModel.del_log, 'mp4', 
                                output_file=output_file, **kwargs)
            print(f'delete logo done, output: {output_file}')
        elif args.function == r'a':
            # 添加水印
            try:
                assert len(args.input) >= 2
            except AssertionError:
                print(f'input file must more than 2')
                exit(-1)
            h264_obj, output_file = _init_tools(args.input[0][0], args.output)
            kwargs['input_img'] = os.path.abspath(args.input[1][0])
            print(f'add logo to video {args.input[0][0]} ...')
            if position_x is not None and position_y is not None:
                kwargs['img_position_x'] = position_x
                kwargs['img_position_y'] = position_y
                _h264_workflow_base(h264_obj, FfmpegCmdModel.logo_video_fix, 'mp4', 
                                    output_file=output_file, **kwargs)
            else:
                _h264_workflow_base(h264_obj, FfmpegCmdModel.logo_video_move, 'mp4', 
                                    output_file=output_file, **kwargs)
            print(f'add logo done, output: {output_file}')
        elif args.function == r'g':
            # 生成gif
            h264_obj, output_file = _init_tools(args.input[0][0], args.output)
            kwargs['v_frame'] = giframe
            print(f'make gif from video {args.input[0][0]} ...')
            _h264_workflow_base(h264_obj, FfmpegCmdModel.create_gif, 'gif', 
                                output_file=output_file, **kwargs)
            print(f'make gif done, output: {output_file}')
        elif args.function == r'h':
            # ts切片
            h264_obj, output_file = _init_tools(args.input[0][0], args.output)
            if not os.path.isdir(output_file):
                print(f'{output_file} must be a dir')
                exit(-1)
            print(f'make ts from video {args.input[0][0]} ...')
            m3u8_out = _h264_workflow_base(h264_obj, FfmpegCmdModel.hls_video, 'm3u8', 
                                           dest_dir=output_file, **kwargs)
            print(f'make ts done, output : {m3u8_out}')
    except KeyboardInterrupt:
        simple_run_cmd(f'rm -rf {args.output}')
        print(r'use interrupt proccess')
        exit(1)
    exit(0)