最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Python將視頻分成幀,將圖片幀合成視頻

2021-06-19 00:44 作者:泉來啦  | 我要投稿

1.將視頻切成圖片


import os
import CV2

"""
功能:將視頻轉(zhuǎn)成圖片(提取視頻的每一幀圖片)
???? 1.能夠設(shè)置多少幀提取一幀圖片
???? 2.可以設(shè)置輸出圖片的大小及灰度圖
???? 3.手動設(shè)置輸出圖片的命名格式
"""
def ExtractVideoFrame(video_input,output_path):
??? # 輸出文件夾不存在,則創(chuàng)建輸出文件夾
??? if not os.path.exists(output_path):
??????? os.mkdir(output_path)

??? times = 0?????????????? # 用來記錄幀
??? frame_frequency = 10??? # 提取視頻的頻率,每frameFrequency幀提取一張圖片,提取完整視頻幀設(shè)置為1
??? count = 0?????????????? # 計數(shù)用,分割的圖片按照count來命名
??? cap = CV2.VideoCapture(video_input)? # 讀取視頻文件

??? print('開始提取', video_input, '視頻的圖片')
??? while True:
??????? times += 1
??????? res, image = cap.read()????????? # 讀出圖片。res表示是否讀取到圖片,image表示讀取到的每一幀圖片
??????? if not res:
??????????? print('圖片提取結(jié)束')
??????????? break
??????? if times % frame_frequency == 0:
??????????? # picture_gray = CV2.cvtColor(image, CV2.COLOR_BGR2GRAY)? # 將圖片轉(zhuǎn)成灰度圖
??????????? # image_resize = CV2.resize(image, (368, 640))??????????? # 修改圖片的大小
??????????? img_name = str(count).zfill(6)+'.jpg'
??????????? CV2.imwrite(output_path + os.sep + img_name, image)
??????????? count += 1

??????????? print(output_path + os.sep + img_name)? # 輸出提示
??? cap.release()

"""
功能:獲取視頻的指定幀并進行顯示
"""
def ShowSpecialFrame(file_path,frame_index):
??? cap = CV2.VideoCapture(file_path)? # 讀取視頻文件
??? cap.set(CV2.CAP_PROP_POS_FRAMES, float(frame_index))
??? if cap.isOpened():? # 判斷是否正常打開
??????? rval, frame = cap.read()
??????? CV2.imshow("image:"+frame_index,frame)
??????? CV2.waitKey()
??? cap.release()

"""
功能:切割視頻的指定幀。比如切割視頻從100幀到第200幀的圖片
???? 1.能夠設(shè)置多少幀提取一幀圖片
???? 2.可以設(shè)置輸出圖片的大小及灰度圖
???? 3.手動設(shè)置輸出圖片的命名格式
"""
def ExtractVideoBySpecialFrame(video_input,output_path,start_frame_index,end_frame_index = -1):
??? # 輸出文件夾不存在,則創(chuàng)建輸出文件夾
??? if not os.path.exists(output_path):
??????? os.mkdir(output_path)

??? cap = CV2.VideoCapture(video_input) # 讀取視頻文件
??? cap.set(CV2.CAP_PROP_POS_FRAMES, float(start_frame_index))??? # 從指定幀開始讀取文件
??? times = 0?????????????????????????? # 用來記錄幀
??? frame_frequency = 10??????????????? # 提取視頻的頻率,每frameFrequency幀提取一張圖片,提取完整視頻幀設(shè)置為1
??? count = 0?????????????????????????? # 計數(shù)用,分割的圖片按照count來命名

??? # 未給定結(jié)束幀就從start_frame_index幀切割到最后一幀
??? if end_frame_index == -1:
??????? print('開始提取', video_input, '視頻從第',start_frame_index,'幀到最后一幀的圖片!!')
??????? while True:
??????????? times += 1
??????????? res, image = cap.read()? # 讀出圖片
??????????? if not res:
??????????????? print('圖片提取結(jié)束?。?#39;)
??????????????? break
??????????? if times % frame_frequency == 0:
??????????????? # picture_gray = CV2.cvtColor(image, CV2.COLOR_BGR2GRAY)? # 將圖片轉(zhuǎn)成灰度圖
??????????????? # image_resize = CV2.resize(image, (368, 640))??????????? # 修改圖片的大小
??????????????? img_name = str(count).zfill(6) + '.jpg'
??????????????? CV2.imwrite(output_path + os.sep + img_name, image)
??????????????? count += 1
??????????????? print(output_path + os.sep + img_name)? # 輸出提示
??? else:
??????? print('開始提取', video_input, '視頻從第', start_frame_index, '幀到第',end_frame_index,'幀的圖片?。?#39;)
??????? k = end_frame_index - start_frame_index + 1
??????? while(k >= 0):
??????????? times += 1
??????????? k -= 1
??????????? res, image = cap.read()? # 讀出圖片
??????????? if not res:
??????????????? print('圖片提取結(jié)束?。?#39;)
??????????????? break
??????????? if times % frame_frequency == 0:
??????????????? # picture_gray = CV2.cvtColor(image, CV2.COLOR_BGR2GRAY)? # 將圖片轉(zhuǎn)成灰度圖
??????????????? # image_resize = CV2.resize(image, (368, 640))??????????? # 修改圖片的大小
??????????????? img_name = str(count).zfill(6) + '.jpg'
??????????????? CV2.imwrite(output_path + os.sep + img_name, image)
??????????????? count += 1
??????????????? print(output_path + os.sep + img_name)? # 輸出提示
??????? print('圖片提取結(jié)束??!')
??? cap.release()


if __name__ == "__main__":
??? # 視頻路徑
??? video_input = 'E://bilibili錄屏//皮斷腿.mp4'
??? # 圖片輸出路徑
??? output_path = r'E:\ps\test'

??? # 提取視頻圖片
??? ExtractVideoFrame(video_input, output_path)

??? # 顯示視頻第100幀的圖片
??? # ShowSpecialFrame(video_input, 1500)

??? # 獲取視頻第100幀到第200幀的圖片
??? #ExtractVideoBySpecialFrame(video_input, output_path, 100, 200)

2.將圖片幀合成視屏


# @description:把照片合成視頻。

import os
import CV2
import uuid


def read_picture():
??? path = 'E://ps//test' #input('請輸入視頻的路徑[eg:D:\Image\BaiDuImgDownLoad\liuhai]: ')
??? file_list = os.listdir(path)

??? fps = 3? # 視頻每秒多少幀
??? height = 1280
??? weight = 720
??? size = (int(height), int(weight))? # 需要轉(zhuǎn)為視頻的圖片的尺寸
??? return [path, fps, size, file_list]


def write_video():
??? path, fps, size, file_list = read_picture()
??? # AVI格式編碼輸出 XVID
??? four_cc = CV2.VideoWriter_fourcc(*'XVID')
??? save_path = path + '\\' + '%s.avi' % str(uuid.uuid1())
??? video_writer = CV2.VideoWriter(save_path, four_cc, float(fps), size)
??? # 視頻保存在當前目錄下
??? for item in file_list:
??????? if item.endswith('.jpg') or item.endswith('.png'):
??????????? # 找到路徑中所有后綴名為.png的文件,可以更換為.jpg或其它
??????????? item = path + '\\' + item
??????????? img = CV2.imread(item)
??????????? re_pics = CV2.resize(img, size, interpolation=CV2.INTER_CUBIC)? # 定尺寸
??????????? if len(re_pics):
??????????????? video_writer.write(re_pics)

??? video_writer.release()
??? CV2.destroyAllWindows()


if __name__ == '__main__':
??? write_video()

Python將視頻分成幀,將圖片幀合成視頻的評論 (共 條)

分享到微博請遵守國家法律
麟游县| 兴业县| 涿州市| 包头市| 曲沃县| 灌阳县| 建瓯市| 玉山县| 新安县| 普宁市| 司法| 铜梁县| 玉环县| 阳曲县| 杭州市| 伊金霍洛旗| 白沙| 陵川县| 元氏县| 仲巴县| 日照市| 宁晋县| 中方县| 富阳市| 龙胜| 蛟河市| 桦川县| 永年县| 宁阳县| 曲阜市| 社旗县| 如东县| 鞍山市| 浏阳市| 治县。| 榆中县| 灵寿县| 永仁县| 盘锦市| 灵宝市| 南岸区|