您的位置:首页 > 汽车 > 新车 > 自定义时长裁切视频

自定义时长裁切视频

2024/9/8 12:32:23 来源:https://blog.csdn.net/Ppandaer/article/details/140669572  浏览:    关键词:自定义时长裁切视频
  1. 人脸检测与定位:

find_host_face_location:在视频的前几秒内快速检测并定位主持人的人脸,缩小帧尺寸以提高处理速度。

  1. 裁剪框计算:

calculate_cropping_box:基于检测到的人脸位置,计算一个适合的裁剪框,确保主持人的人脸处于视频画面的中心位置。

  1. 动态帧率采样与首次人脸检测时间:

find_first_face_time:动态调整帧率采样(sample_rate),优化处理速度,找到视频中首次出现人脸的确切时间。

  1. 视频裁剪与帧率调整:

process_video:综合以上功能,裁剪视频以保留从首次出现人脸开始的10秒片段,调整视频尺寸和帧率为标准格式,输出处理后的视频。

#  python data_utils/video/cut_crop_fps_1.1.pyimport os
import cv2
import math
import numpy as np
import face_recognition
from moviepy.editor import VideoFileClip, concatenate_videoclips
from tqdm import tqdmdef find_host_face_location(video_path):""" 在视频的前几秒内检测并返回主持人面部的大致位置 """cap = cv2.VideoCapture(video_path)found_face = Falsehost_face_location = Nonewhile cap.isOpened():ret, frame = cap.read()if not ret:break# 缩小帧尺寸以加快处理速度small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)rgb_small_frame = small_frame[:, :, ::-1]# 检测人脸face_locations = face_recognition.face_locations(rgb_small_frame)if face_locations:# 取第一张脸的位置,假设主持人位于视频画面的中心位置附近host_face_location = face_locations[0]# 将位置放大回原始大小host_face_location = (host_face_location[0]*4, host_face_location[1]*4, host_face_location[2]*4, host_face_location[3]*4)found_face = Truebreakcap.release()return host_face_location if found_face else Nonedef calculate_cropping_box(face_location, frame_shape):""" 根据主持人面部位置计算裁剪框 """top, right, bottom, left = face_locationcenter_x, center_y = (left + right) // 2, (top + bottom) // 2half_width, half_height = 256, 256left_cropped = max(center_x - half_width, 0)top_cropped = max(center_y - half_height, 0)right_cropped = min(center_x + half_width, frame_shape[1])bottom_cropped = min(center_y + half_height, frame_shape[0])return (top_cropped, right_cropped, bottom_cropped, left_cropped)def find_first_face_time(video_path, sample_rate, min_confidence=0.5): """ 找到视频中第一次出现人脸的时间戳,优化处理速度 """cap = cv2.VideoCapture(video_path)fps = cap.get(cv2.CAP_PROP_FPS)first_face_time = Nonewhile cap.isOpened():ret, frame = cap.read()if not ret:break# 按照sample_rate进行帧率采样if cap.get(cv2.CAP_PROP_POS_FRAMES) % sample_rate != 0:continuetimestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000  # Convert to seconds# 缩小帧尺寸以加快处理速度small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)rgb_small_frame = small_frame[:, :, ::-1]# 检测人脸face_locations = face_recognition.face_locations(rgb_small_frame, model='hog')  # 可以选择 'cnn' 或 'hog'if face_locations:if not first_face_time:first_face_time = timestampbreakcap.release()return first_face_timedef process_video(input_path, output_path):""" 处理视频,裁剪并调整帧率 """# 检测主持人面部位置host_face_location = find_host_face_location(input_path)if host_face_location is None:print(f"No face detected in video {input_path}")return# 读取视频,获取视频的宽度、高度和帧率clip = VideoFileClip(input_path)frame_shape = clip.size[::-1]  # 电影剪辑的尺寸是(width, height),我们需要(height, width)fps = clip.fps# 动态设置sample_rate,例如,我们希望每秒检测10次desired_detection_frequency = 10  # 每秒检测次数sample_rate = int(fps / desired_detection_frequency)# 确保sample_rate至少为1,避免除以0的情况sample_rate = max(sample_rate, 1)# 计算裁剪框cropping_box = calculate_cropping_box(host_face_location, frame_shape)# 找到第一次出现人脸的时间# 使用动态设置的sample_rate调用find_first_face_timefirst_face_time = find_first_face_time(input_path, sample_rate=sample_rate)print(f"First face time: {first_face_time}")# 裁剪视频以保留从第一次出现人脸开始的10秒start_trim = math.ceil(first_face_time)  # 向上取整end_trim = min(start_trim + 10, clip.duration)  # 确保不超过视频总时长print(f"Start trim: {start_trim}, End trim: {end_trim}")trimmed_clip = clip.subclip(start_trim, end_trim)# 裁剪视频cropped_clip = trimmed_clip.crop(x1=cropping_box[3], y1=cropping_box[0], x2=cropping_box[1], y2=cropping_box[2])cropped_clip = cropped_clip.resize((512, 512))# 调整帧率cropped_clip = cropped_clip.set_fps(25)# 保存最终视频cropped_clip.write_videofile(output_path, codec='libx264', audio_codec='aac')# 清理资源cropped_clip.close()if __name__ == "__main__":# 遍历指定文件夹中的所有视频文件input_folder = 'video/HDTF'video_files = [f for f in os.listdir(input_folder) if f.endswith(('.mp4', '.avi', '.mkv'))]# 创建一个进度条with tqdm(total=len(video_files), desc="Processing Videos") as pbar:for filename in video_files:input_path = os.path.join(input_folder, filename)print("input_path:", input_path)# 动态生成输出文件名output_filename = f"{os.path.splitext(filename)[0]}_p{os.path.splitext(filename)[1]}"output_path = os.path.join(input_folder, output_filename)# 处理视频process_video(input_path, output_path)# 更新进度条pbar.update(1)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com