代码细节:1、视频帧率取决不同opencv版本 2、从VideoCapture读出来的文件,没有了还能读!!所以最后一帧是None,用frame_s.pop()弹出3、注意内存各种占满所以对Moviepy的视频做.close()。对opencv()做.release()和cv2.destroyAllWindows()

4、代码设计,video ---> frame; frame ---> visual; video ---->audio; visual + audio ---> video.

from moviepy.editor import *
import xml.etree.ElementTree as ET
import os
import cv2
import glob
import shutil
import pdbdef draw_redNose_in_video(correspond_video, xml_root, probe):replaced = os.path.dirname(os.path.dirname(os.path.dirname(video_dst_multip)))print(correspond_video.replace(replaced, ''))index_s = []point_s = []# pdb.set_trace()src = glob.glob(correspond_video+'*')[0]# decoding xmlfor index, type_tag in enumerate(xml_root.findall(probe)):point = type_tag.find('point').textif index == len(xml_root.findall(probe)) - 1:frame_index = int(type_tag.find('frameIndex').text)  # The last frame may be out of the range.else:frame_index = int(type_tag.find('frameIndex').text) + 1index_s.append(frame_index)point_xy = point.split(',')point_s.append(point_xy)vidcap = cv2.VideoCapture(src)# Find OpenCV version(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')if int(major_ver) < 3:fps = vidcap.get(cv2.cv.CV_CAP_PROP_FPS)else:fps = vidcap.get(cv2.CAP_PROP_FPS)# video --> framesframe_s = []if vidcap.isOpened():success = Truewhile success:success, image = vidcap.read()if cv2.waitKey(10) == 27:breakframe_s.append(image)# The last is None rather than a Image, so we drop it.frame_s.pop()else:print('read error')_, img_w, _ = frame_s[0].shape# frame --> visualif len(index_s) >= 2:min_index = index_s[0]index_stride = index_s[1] - index_s[0]max_index = min( index_s[-1] + index_stride, len(frame_s))index_s.append(max_index)# ## either:1/2:# head_index = index_s.pop(0)# tail_index = index_s.pop(0)## x_y = point_s.pop(0)# img_array = []## for id, item in enumerate(frame_s):#     rgb_item = cv2.cvtColor(item, cv2.COLOR_BGR2RGB)  # BGR[...,::-1] --> RGB#     if min_index <= id < max_index:#         if id >= tail_index:#             head_index = tail_index#             tail_index = index_s.pop(0)#             x_y = point_s.pop(0)##         if head_index <= id < tail_index:##             x_y = tuple([int(e) for e in x_y]) # list(x_y) --> tuple(x_y)#             cv2.circle(rgb_item, x_y, int(img_w/300.), (255, 0, 0), int(img_w/300.))##     img_array.append(rgb_item)## either:2/2:first_head = index_s[0]first_tail = index_s[1]last_head = index_s[-2]last_tail = index_s[-1]img_array = []for id, item in enumerate(frame_s):rgb_item = cv2.cvtColor(item, cv2.COLOR_BGR2RGB)  # BGR[...,::-1] --> RGBif first_head <= id < first_tail: # or last_head <= id < last_tail:if id < first_tail:x_y = point_s[0]else:x_y = point_s[-1]x_y = tuple([int(e) for e in x_y]) # list(x_y) --> tuple(x_y)cv2.circle(rgb_item, x_y, int(img_w/300.), (255, 0, 0), int(img_w/300.))img_array.append(rgb_item)# pdb.set_trace()drawed_video = ImageSequenceClip(img_array, fps=fps)# visual + audio --> videovideoclip = VideoFileClip(src)videoclip2 = drawed_video.set_audio(videoclip.audio)# Write Videodst = src.replace(video_dir, video_dst_multip)parent_dst = os.path.dirname(dst)if not os.path.exists(parent_dst):os.makedirs(parent_dst)videoclip2.write_videofile(dst)# close allvideoclip.close()videoclip2.close()drawed_video.close()del videoclip.make_framedel videoclip2.make_framedel drawed_video.make_framedel videoclipdel videoclip2del drawed_videocv2.destroyAllWindows()vidcap.release()def copy_video(correspond_video):replaced = os.path.dirname(os.path.dirname(video_dst_multip))print(correspond_video.replace(replaced, ''))src = glob.glob(correspond_video+'*')[0]dst = src.replace(video_dir, video_dst_single)parentDir_dst = os.path.dirname(dst)if not os.path.exists(parentDir_dst):os.makedirs(parentDir_dst)shutil.copy(src, dst)def main(target_xml, video_dir):have = nothave = 0for index_id, (root, dirs, files) in enumerate(os.walk(target_xml)):# if index_id > 73:for xml in files:xml_dir = os.path.join(root,xml)xml_root = ET.parse(xml_dir).getroot()correspond_video = xml_dir.replace(target_xml, video_dir).replace('.xml', '')# To do or not to dosrc1 = glob.glob(correspond_video + '*')[0].replace(video_dir, video_dst_single)src2 = glob.glob(correspond_video + '*')[0].replace(video_dir, video_dst_multip)if not os.path.exists(src1) and not os.path.exists(src2):# some old_xml are not have these filehead = 'videolabel'if xml_root.findall(head):probe = os.path.join(head, 'points/facePoint')else:probe = 'points/facePoint'# some are singlen's xml, some are rednose's xmlif xml_root.findall(probe):# rednose's xmlhave += 1draw_redNose_in_video(correspond_video, xml_root, probe)else:nothave += 1copy_video(correspond_video)# pdb.set_trace()print('have {}'.format(have))print('nothave {}'.format(nothave))if __name__=='__main__':xmls_dir = '/home/debin/Desktop/schedule_management/Ideas/VideoDataset/sub_fromNSFC//Xmls_formNVL/pat1'video_dir = '/home/debin/Desktop/schedule_management/Ideas/VideoDataset/sub_fromNSFC/Videos_formNVL/pat1'video_dst_single = '/home/debin/Desktop/schedule_management/Ideas/VideoDataset/My_draw_Video/Single/pat1'video_dst_multip = '/home/debin/Desktop/schedule_management/Ideas/VideoDataset/My_draw_Video/Red_Node/pat1'main(xmls_dir, video_dir)

Opencv+Moviepy实现涂鸦视频和视频音轨分离合并操作。相关推荐

  1. [Python图像处理] 二十九.MoviePy视频编辑库实现抖音短视频剪切合并操作

    该系列文章是讲解Python OpenCV图像处理知识,前期主要讲解图像入门.OpenCV基础用法,中期讲解图像处理的各种算法,包括图像锐化算子.图像增强技术.图像分割等,后期结合深度学习研究图像识别 ...

  2. python(opencv + pyaudio + moviepy)实现录制音视频文件并合并

    使用opencv录制视频文件 def record_webcam(filename):"""cv2.VideoCapture(0, cv2.CAP_DSHOW)参数1:打 ...

  3. 参考 | ffmpeg 修改多音轨视频的默认音轨

    参考 | ffmpeg 修改多音轨视频的默认音轨 文章目录 参考 | ffmpeg 修改多音轨视频的默认音轨 0. 起因 1. 解决 a. 先安装 ffmpeg b. 修改视频的默认音轨 0. 起因 ...

  4. Moviepy+OpenCV-python 结合进行音视频剪辑处理的一种建议模式

    一.引言 如今短视频和自媒体大行其道,不会点视频剪辑技能都不好说自己会玩自媒体,音视频剪辑工具大受欢迎,作为万能的编程语言 Python,也早就有了自己的音视频剪辑库 Moviepy. MoviePy ...

  5. 【opencv】3.在一个opencv窗口中显示多个视频界面、画箭头、画掉头箭头

    1.在一个opencv窗口中显示不同视频界面 /** * @brief 在一个opencv窗口win_name中显示不同视频界面 * @param img_1 和 img_2 是分别是取自不同视频中的 ...

  6. OpenCV VideoCapture与捕获设备,视频文件或图像序列一起使用的实例

    OpenCV VideoCapture与捕获设备,视频文件或图像序列一起使用的实例 OpenCV VideoCapture与捕获设备,视频文件或图像序列一起使用的实例 OpenCV VideoCapt ...

  7. 从入门到入土:Python实现爬取某站视频|根据视频编号|支持通过视频名称和创作者名称寻找编号|以及python moviepy合并音频视频

    写在前面: 此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) Python实现爬取某站视频|根据视频编号|支持通过视频名称 ...

  8. 使用moviepy快速剪辑和拼接视频

    1 安装moviepy pip install moviepy 2 使用movie进行视频剪辑 from moviepy.editor import * import time# 需要剪辑视频的起始时 ...

  9. moviepy剪切视频spleeter视频降噪-CPUGPU

    文章目录 简介 安装spleeter 代码执行 简介 moviepy官方中文API spleeter官方github 本文视频降噪原理为使用spleeter提取出人声,仅将人声写回视频并保存. 对于A ...

  10. python图像处理opencv笔记(二):视频基本操作

    视频基本操作 视频读取 opencv中通过VideoCaptrue类对视频进行读取操作以及调用摄像头,下面是该类的API: import cv2video = cv2.VideoCapture(0) ...

最新文章

  1. ICRA 2021| 具有在线校准功能的高效多传感器辅助惯性导航系统
  2. python入门买什么书-大学生Python入门什么书好?
  3. 根据id去重_Person Re-ID研究综述
  4. javascript --- ES6模块与CommonJS模块的差异
  5. 【Linux进程、线程、任务调度】一 Linux进程生命周期 僵尸进程的含义 停止状态与作业控制 内存泄漏的真实含义 task_struct以及task_struct之间的关系
  6. 我的世界java什么村民卖地图_《我的世界》推出虚拟货币、商店 玩家可在店中卖地图...
  7. 选择与Git进行提交意味着什么?
  8. 位移的单位符号_,(有符号位移)和(无符号位移)的使用方法,及差别
  9. Xamarin.Android Binding-----百度地图SDK
  10. ECJia 到家 v1.5.1 发布,基于 O2O 的移动电商开源系统
  11. Untiy 接入 移动MM 详解
  12. 欧拉回路和Hanmilton回路
  13. 使用MIPS完成汇编程序——选择排序实现
  14. RAML规范1.0(译文)
  15. set的用法及短语_set的用法和例句
  16. linux查看更多历史记录,查看更多历史,如何查看浏览历史记录
  17. 茅侃侃 | 生亦何哀,死亦何苦
  18. 树莓派一键变身无线路由器
  19. 服务器硬件基础设施,【通讯技术】细节定成败,NFV中的硬件基础设施管理
  20. windows下更改应用程序属性的详细信息

热门文章

  1. SuperMap iClient3D for WebGL实现三维管线分析
  2. Oracle数据库的ORA-00257故障解决过程(转载)
  3. ABBYY FineReader 超强OCR识别软件 V15.0.0 特别版
  4. 文件生成BASE64,base64转文件
  5. icem网格划分如何给内部面网格,ICEM CFD处理混合网格划分中低质量的问题
  6. linux远程拷贝东西
  7. ug10.0安装好了怎么找到
  8. Podfile文件用法详解
  9. 局域网服务器共享不稳定怎么办,局域网计算机文件共享异常解决方案
  10. python矩阵行秩函数_矩阵的秩的性质以及矩阵运算和矩阵的秩的关系