自定义时长裁切视频

  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.py

import os
import cv2
import math
import numpy as np
import face_recognition
from moviepy.editor import VideoFileClip, concatenate_videoclips
from tqdm import tqdm

def find_host_face_location(video_path):
    """ 在视频的前几秒内检测并返回主持人面部的大致位置 """
    cap = cv2.VideoCapture(video_path)
    found_face = False
    host_face_location = None
    
    while 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 = True
            break
    
    cap.release()
    return host_face_location if found_face else None

def calculate_cropping_box(face_location, frame_shape):
    """ 根据主持人面部位置计算裁剪框 """
    top, right, bottom, left = face_location
    center_x, center_y = (left + right) // 2, (top + bottom) // 2
    half_width, half_height = 256, 256
    
    left_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 = None

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        # 按照sample_rate进行帧率采样
        if cap.get(cv2.CAP_PROP_POS_FRAMES) % sample_rate != 0:
            continue

        timestamp = 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 = timestamp
                break

    cap.release()
    return first_face_time

def 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_time
    first_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)
在Android中,可以使用自定义视图和画布来实现图像裁剪。 首先,创建一个自定义的视图类,并在其中重写onDraw()方法。在onDraw()方法中,使用画布(Canvas)对象绘制需要裁剪的图像。然后,使用画布对象的clipRect()方法来指定需要裁剪的区域。最后,使用画布对象的drawBitmap()方法来绘制裁剪后的图像。 以下是一个简单的示例代码: ```java public class CustomImageView extends View { private Bitmap mBitmap; public CustomImageView(Context context, AttributeSet attrs) { super(context, attrs); mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制原始图像 canvas.drawBitmap(mBitmap, 0, 0, null); // 裁剪区域 Rect rect = new Rect(100, 100, 300, 300); canvas.clipRect(rect); // 绘制裁剪后的图像 canvas.drawBitmap(mBitmap, 0, 0, null); } } ``` 在这个示例代码中,我们首先使用BitmapFactory类从资源文件中加载一个图像。然后,在onDraw()方法中,我们使用Canvas对象绘制该图像。接下来,我们创建一个矩形对象,指定需要裁剪的区域。然后,我们使用Canvas对象的clipRect()方法将裁剪区域设置为该矩形。最后,我们再次使用Canvas对象绘制图像,此时只会绘制裁剪区域内的部分。 当然,这只是一个简单的示例。你可以根据自己的需求和想象力来实现更复杂的图像裁剪效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值