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}')
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}')
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}')
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}')
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}')
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])
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])
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
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
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}')
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}')
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}')
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}')
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}')
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)
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}')