赞
踩
-
- """
- 用于获取多个关键词的视频摘要
- 本代码还是有缺陷的,因为选择了几个for循环,导致时间还是挺慢的。
- """
-
- import cv2 as cv
- import numpy as np
- import re
- import os
- from PIL import Image
- video_file = cv.VideoCapture(r"E:\900秒嫦娥落月.mp4")
- fps = video_file.get(5)
- print("该视频的帧为{}".format(fps))
- #==================================================================================#
- """
- 实验阶段:该内容是用来获取想要寻找的关键字段,在视频中时间位置,并进行截取视频
- 最终阶段:进行文本匹配,谁的相似度最高,就选择哪一段进行截取,
- 而在此任务中涉及到的知识很多:文本相似度匹配、近义词训练、如何将txt文件的字幕和时间分割再还原等等问题
- """
- #name=input("请输入提取内容:")
- # name=input("请输入你所需要寻找的关键字段或者关键内容:")
- # with open("CHS_嫦娥奔月.txt","r",encoding="utf-8") as f:
- # flie_data=f.readlines()
- # for i,readline in enumerate(flie_data):
- # #print(readline)
- # # matchObj=re.match(name,readline)#匹配时,如果第一个字符不匹配,就会发生匹配失败
- # matchObj = re.search(name, readline)
- # print(matchObj)
- # if matchObj is not None:
- # print("匹配字符所在的位置{}".format(i))
- # print("所在的时间段为{}".format(flie_data[i-1]))
- # break
- list_time=[]
- list_frames_time=[]
- name_all=input("请输入需要提取的关键词(用|分开):")
- name_all=name_all.split("|")
- # name_all=["燃料","失败"]
- with open(r"D:\PycharmProjects\pythonProject\CHS_嫦娥奔月.txt","r",encoding="utf-8") as f:
- flie_data=f.readlines()
- for name in name_all:
- for i,readline in enumerate(flie_data):
- #print(readline)
- # matchObj=re.match(name,readline)#匹配时,如果第一个字符不匹配,就会发生匹配失败
- matchObj = re.search(name, readline)
- print(matchObj)
- if matchObj is not None:
- list_time.append(flie_data[i-1])
- print("匹配字符所在的位置{}".format(i))
- print("所在的时间段为{}".format(flie_data[i-1]))
- list_time.sort()#将时间进行排序(从小到大)
- print("需要截取的视频时间",list_time)
- # =============================================================================#
- # 首先弄人工输入,并将帧和时间进行转换
- # start_frame = input("请输入开始帧数,(输入格式为时:分:秒,毫秒或者帧):")
- for start_frame in list_time:
- #start_frame=flie_data[i-1]
- if "-->" in start_frame:
- start_frame_1 = start_frame.split(" --> ")
- start_frame = start_frame_1[0]
- end_frame = start_frame_1[1]
- print("起始时间-->终止时间")
- else:
- end_frame = input("请结束开始帧数,(输入格式为时:分:秒,毫秒或者帧):")
- """
- 如果得到了是时间格式就进行转换成帧格式
- 输入的是时:分:秒.毫秒
- 1时=60分=3600秒=3600000毫秒
- 如果得到的是帧格式,那正好不用转换
- """
- # start_frame_spilt=start_frame.split(":")
- # ==============================================================================#
- if ":" in start_frame:
- print("输入的是时间制!")
- name_str = start_frame.split(":")
- start_frame = (int(name_str[0]) * 3600 + int(name_str[1]) * 60 + float(name_str[2].replace(",", "."))) * fps
- list_frames_time.append(start_frame)
- else:
- print("输入的值十进制(帧)!")
- start_frame = int(start_frame)
- if ":" in end_frame:
- print("输入的是时间制!")
- name_str = end_frame.split(":")
- end_frame = (int(name_str[0]) * 3600 + int(name_str[1]) * 60 + float(name_str[2].replace(",", "."))) * fps
- list_frames_time.append(end_frame)
- else:
- print("输入的值十进制(帧)!")
- end_frame = int(end_frame)
- # ==============================================================================================#
- """
- 如果前后的时间比较小,就是帧数小于1,那么无法截到图,所以直接截前后5帧来进行填充
- """
- if (end_frame - start_frame) < 1:
- start_frame = start_frame - 5
- end_frame = end_frame + 5
- else:
- pass
- print(list_frames_time)
- count_frame=0
- for i in range(0,len(list_frames_time),2):
- start_frame=list_frames_time[i]
- end_frame=list_frames_time[i+1]
- success, frames = video_file.read()
- while success:
- success, frames = video_file.read()
- count_frame += 1
- if count_frame >= start_frame and count_frame <= end_frame:
- print("截取第{}帧".format(count_frame))
- cv.imwrite(r"D:\PycharmProjects\pythonProject\feiji\frames\image{}.jpg".format(count_frame), frames)
- if count_frame > end_frame:
- break
-
- print("开始合成视频")
- im_list = os.listdir("D:/PycharmProjects/pythonProject/feiji/frames/")
- #im_list.sort(key=lambda x: int(x.replace("frame", "").split('.')[0])) # 最好再看看图片顺序对不
- img = Image.open(os.path.join("D:/PycharmProjects/pythonProject/feiji/frames/", im_list[0]))
- img_size = img.size # 获得图片分辨率,im_dir文件夹下的图片分辨率需要一致
-
- # fourcc = cv2.cv.CV_FOURCC('M','J','P','G') #opencv版本是2
- fourcc = cv.VideoWriter_fourcc(*'MJPG') # opencv版本是3
- videoWriter = cv.VideoWriter(r"D:\PycharmProjects\pythonProject\feiji\frames\test.avi", fourcc, fps, img_size)
- # count = 1
- for i in im_list:
- im_name = os.path.join("D:/PycharmProjects/pythonProject/feiji/frames/" + i)
- frame = cv.imdecode(np.fromfile(im_name, dtype=np.uint8), -1)
- videoWriter.write(frame)
- # count+=1
- # if (count == 200):
- # print(im_name)
- # break
- videoWriter.release()
- print('finish')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。