根据主视频,自动设置片头的分辨率、Fps、编码器:
- 前提:
- 安装:ffmpeg
- 安装:python库 opencv-python
pip3 install opencv-python
def set_start_video(self, video_path, start_video_path): """ :param video_path: 原始mp4 绝对路径 :param start_video_path: 片头的绝对路径 :return: """ video = cv2.VideoCapture(f"{video_path}")
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = video.get(cv2.CAP_PROP_FPS)
fps_a = '%.2f' % fps
video.release() cv2.destroyAllWindows() command = f"ffprobe -v quiet -select_streams v:0 -show_entries stream=codec_name -of default '{video_path}/v1.mp4'" result = subprocess.run(command, shell=True, stdout=subprocess.PIPE) encoder = result.stdout.decode().strip() new_encoder = re.search("codec_name=(?P<codec_name>.*?)\n", encoder) cmd_str = f"ffmpeg -i {start_video_path} -vf scale={width}:{height} -r {fps_a} -acodec aac -vcodec {new_encoder.group('codec_name')} {video_path}/xxx.ts" os.system(cmd_str)
|