• python利用ffmpeg将一段大视频等份的切成多个小视频段


    在剪辑视频的过程中发现部分手机拍摄的视频是带有rotate旋转矫正参数的,一般的opencv脚本剪辑出来的视频是歪的。查询了大量的资料找到一种使用ffmpeg剪辑的方法,将opencv和ffmpeg结合使用可以剪辑目前绝大多数的手机视频。

    def cutVideo(path,filename):
        video_full_path = path + r'\' +filename
        video_full_path_new = path + r'\' +'new_'+filename
        cap = cv2.VideoCapture(video_full_path)
        cap.isOpened()
        width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    
        print(width, height)
    
        if cap.isOpened():  # 当成功打开视频时cap.isOpened()返回True,否则返回False
            # get方法参数按顺序对应下表(从0开始编号)
            rate = cap.get(5)  # 帧速率
            FrameNumber = int(cap.get(7))  # 视频文件的帧数
            duration = FrameNumber / rate  # 帧速率/视频总帧数 是时间,除以60之后单位是分钟
            len = int(duration)
            fps = int(rate)
            print(rate, FrameNumber)
    
            if (width > height):
    
                for i in range(0,len):
                    video_full_path_new = path + r'\' + str(i) + filename
                    print(i)
                    cmd = 'ffmpeg -i {0} -vcodec copy -acodec copy -ss 00:00:0{1} -to 00:00:0{2} {3} -y'.format(video_full_path,str(i),str(i+1),video_full_path_new)
                    os.system(cmd)
    
            else:
                i = 0
                while (True):
                    success, frame = cap.read()
                    if success:
                        i += 1
                        # print('i = ', i)
                        if (i % fps == 1):
                            videoWriter = cv2.VideoWriter(path + '/' + str(i) + filename,
                                                          cv2.VideoWriter_fourcc('D', 'I', 'V', 'X'), fps,
                                                          (int(width), int(height)))
                            videoWriter.write(frame)
                        else:
                            videoWriter.write(frame)
                    else:
                        print('end')
                        break
    
    
    
        cap.release()
    

      

  • 相关阅读:
    循环结构
    位运算符
    Switch 选择结构
    if结构和逻辑运算符
    变量和运算符
    [luogu1090 SCOI2003] 字符串折叠(区间DP+hash)
    [luogu2329 SCOI2005] 栅栏(二分+搜索)
    [luogu 4886] 快递员
    [luogu4290 HAOI2008]玩具取名(DP)
    [luogu2624 HNOI2008]明明的烦恼 (prufer+高精)
  • 原文地址:https://www.cnblogs.com/tianyun5115/p/12056840.html
Copyright © 2020-2023  润新知