python ffmpeg任意提取視頻幀的方法
環(huán)境準(zhǔn)備
1、安裝 FFmpeg
2、安裝 ffmpeg-python
pip3 install ffmpeg-python
3、【可選】安裝 opencv-python
pip3 install opencv-python
4、【可選】安裝 numpy
pip3 install numpy
視頻幀提取
準(zhǔn)備視頻素材
抖音視頻素材下載:https://anoyi.com/dy/top
基于視頻幀數(shù)提取任意一幀
import ffmpeg
import numpy
import cv2
import sys
import random
def read_frame_as_jpeg(in_file, frame_num):
"""
指定幀數(shù)讀取任意幀
"""
out, err = (
ffmpeg.input(in_file)
.filter('select', 'gte(n,{})'.format(frame_num))
.output('pipe:', vframes=1, format='image2', vcodec='mjpeg')
.run(capture_stdout=True)
)
return out
def get_video_info(in_file):
"""
獲取視頻基本信息
"""
try:
probe = ffmpeg.probe(in_file)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
if video_stream is None:
print('No video stream found', file=sys.stderr)
sys.exit(1)
return video_stream
except ffmpeg.Error as err:
print(str(err.stderr, encoding='utf8'))
sys.exit(1)
if __name__ == '__main__':
file_path = '/Users/admin/Downloads/拜無(wú)憂.mp4'
video_info = get_video_info(file_path)
total_frames = int(video_info['nb_frames'])
print('總幀數(shù):' + str(total_frames))
random_frame = random.randint(1, total_frames)
print('隨機(jī)幀:' + str(random_frame))
out = read_frame_as_jpeg(file_path, random_frame)
image_array = numpy.asarray(bytearray(out), dtype="uint8")
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
cv2.imshow('frame', image)
cv2.waitKey()
基于時(shí)間提取任意一幀
import ffmpeg
import numpy
import cv2
import sys
import random
def read_frame_by_time(in_file, time):
"""
指定時(shí)間節(jié)點(diǎn)讀取任意幀
"""
out, err = (
ffmpeg.input(in_file, ss=time)
.output('pipe:', vframes=1, format='image2', vcodec='mjpeg')
.run(capture_stdout=True)
)
return out
def get_video_info(in_file):
"""
獲取視頻基本信息
"""
try:
probe = ffmpeg.probe(in_file)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
if video_stream is None:
print('No video stream found', file=sys.stderr)
sys.exit(1)
return video_stream
except ffmpeg.Error as err:
print(str(err.stderr, encoding='utf8'))
sys.exit(1)
if __name__ == '__main__':
file_path = '/Users/admin/Downloads/拜無(wú)憂.mp4'
video_info = get_video_info(file_path)
total_duration = video_info['duration']
print('總時(shí)間:' + total_duration + 's')
random_time = random.randint(1, int(float(total_duration)) - 1) + random.random()
print('隨機(jī)時(shí)間:' + str(random_time) + 's')
out = read_frame_by_time(file_path, random_time)
image_array = numpy.asarray(bytearray(out), dtype="uint8")
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
cv2.imshow('frame', image)
cv2.waitKey()
相關(guān)資料
https://github.com/kkroening/ffmpeg-python/tree/master/examples
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pycharm及python安裝詳細(xì)步驟及PyCharm配置整理(推薦)
這篇文章主要介紹了Pycharm及python安裝詳細(xì)步驟以及PyCharm配置整理,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python中將圖像轉(zhuǎn)換為PDF的方法實(shí)現(xiàn)
本文主要介紹了Python中將圖像轉(zhuǎn)換為PDF的方法實(shí)現(xiàn),主要使用img2pdf和PyPDF2軟件包,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
Python中2種常用數(shù)據(jù)可視化庫(kù)Bokeh和Altair使用示例詳解
本文對(duì)Python中兩個(gè)常用的數(shù)據(jù)可視化庫(kù)?Bokeh?和?Altair?進(jìn)行了比較和探討,通過(guò)對(duì)它們的特點(diǎn)、優(yōu)缺點(diǎn)以及使用示例的詳細(xì)分析,讀者可以更好地了解這兩個(gè)庫(kù)的功能和適用場(chǎng)景,從而更好地選擇合適的庫(kù)來(lái)進(jìn)行數(shù)據(jù)可視化工作,感興趣的朋友跟隨小編一起看看吧2024-04-04
PyCharm無(wú)法引用自身項(xiàng)目解決方式
今天小編就為大家分享一篇PyCharm無(wú)法引用自身項(xiàng)目解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
利用python腳本提取Abaqus場(chǎng)輸出數(shù)據(jù)的代碼
這篇文章主要介紹了利用python腳本提取Abaqus場(chǎng)輸出數(shù)據(jù),利用python腳本對(duì)Abaqus進(jìn)行數(shù)據(jù)提取時(shí),要對(duì)python腳本做前步的導(dǎo)入處理,本文通過(guò)實(shí)例代碼詳細(xì)講解需要的朋友可以參考下2022-11-11
使用pandas實(shí)現(xiàn)csv/excel sheet互相轉(zhuǎn)換的方法
今天小編就為大家分享一篇使用pandas實(shí)現(xiàn)csv/excel sheet互相轉(zhuǎn)換的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

