不为别的,只为做一个连自己都羡慕的人

python截取视频中的某一段,保存为avi结尾的视频

# 功能:对视频文件进行剪切。
# 剪切指定长度的视频,选择要裁剪的视频,选择开始时间点和停止时间点即可。
# 将处理后的视频保存为output.avi文件
import cv2  # OpenCV
import tkinter.filedialog  # Python文件对话框

def samllVideoGif(_videoPath,_videoGifPath):
    # _videoPath = "C:\\Users\\sswc\\Desktop\\gai2\\public\\showPdf\\6f3db1f9-e247-4aa5-bca7-93bae62a0079.mp4"
    # _videoGifPath = "C:\\Users\\sswc\\Desktop\\gai2\\public\\showPdf\\6f3db1f9-e247-4aa5-bca7-93bae62a0079.avi"
    cap = cv2.VideoCapture(_videoPath)  # 打开视频文件
    frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)  # 获得视频文件的帧数
    fps = cap.get(cv2.CAP_PROP_FPS)  # 获得视频文件的帧率
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)  # 获得视频文件的帧宽
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # 获得视频文件的帧高

    # 创建保存视频文件类对象
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(_videoGifPath, fourcc, fps, (int(width), int(height)))

    # 计算视频长度/s
    video_length = frames / fps
    # print('start and stop must < %.1f' % video_length)  # 提示用户输入变量的范围
    # start = float(input('Input an start time/s:'))
    # stop = float(input('Input an stop time/s:'))
    # print('start and stop must < %.1f' % video_length)  # 提示用户输入变量的范围
    start = 0
    stop = 10
    # 设置帧读取的开始位置
    cap.set(cv2.CAP_PROP_POS_FRAMES, start * fps)
    pos = cap.get(cv2.CAP_PROP_POS_FRAMES)  # 获得帧位置
    while (pos <= stop * fps):
        ret, frame = cap.read()  # 捕获一帧图像
        out.write(frame)  # 保存帧
        pos = cap.get(cv2.CAP_PROP_POS_FRAMES)

    cap.release()
    out.release()

 

posted @ 2020-09-03 16:02  升级打怪  阅读(1454)  评论(0编辑  收藏  举报