Python實(shí)現(xiàn)獲取視頻時(shí)長(zhǎng)功能
前言
本文提供獲取視頻時(shí)長(zhǎng)的python代碼,精確到毫秒,一如既往的實(shí)用主義。
環(huán)境依賴
?ffmpeg環(huán)境安裝,可以參考:windows ffmpeg安裝部署
本文主要使用到的不是ffmpeg,而是ffprobe也在上面這篇文章中的zip包中。

代碼
不廢話,上代碼。
#!/user/bin/env python
# coding=utf-8
"""
@project : csdn
@author : 劍客阿良_ALiang
@file : get_video_duration.py
@ide : PyCharm
@time : 2021-12-23 13:52:33
"""
import os
import subprocess
def get_video_duration(video_path: str):
ext = os.path.splitext(video_path)[-1]
if ext != '.mp4' and ext != '.avi' and ext != '.flv':
raise Exception('format not support')
ffprobe_cmd = 'ffprobe -i {} -show_entries format=duration -v quiet -of csv="p=0"'
p = subprocess.Popen(
ffprobe_cmd.format(video_path),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
out, err = p.communicate()
print("subprocess 執(zhí)行結(jié)果:out:{} err:{}".format(out, err))
duration_info = float(str(out, 'utf-8').strip())
return int(duration_info * 1000)
if __name__ == '__main__':
print('視頻的duration為:{}ms'.format(get_video_duration('D:/tmp/100.mp4')))
代碼說(shuō)明:
1、對(duì)視頻的后綴格式做了簡(jiǎn)單的校驗(yàn),如果需要調(diào)整可以自己調(diào)整一下。
2、對(duì)輸出的結(jié)果做了處理,輸出int類型的數(shù)據(jù),方便使用。
驗(yàn)證一下
準(zhǔn)備的視頻如下:

驗(yàn)證一下

補(bǔ)充
Python實(shí)現(xiàn)獲取視頻fps
#!/user/bin/env python
# coding=utf-8
"""
@project : csdn
@author : 劍客阿良_ALiang
@file : get_video_fps.py
@ide : PyCharm
@time : 2021-12-23 11:21:07
"""
import os
import subprocess
def get_video_fps(video_path: str):
ext = os.path.splitext(video_path)[-1]
if ext != '.mp4' and ext != '.avi' and ext != '.flv':
raise Exception('format not support')
ffprobe_cmd = 'ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate {}'
p = subprocess.Popen(
ffprobe_cmd.format(video_path),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
out, err = p.communicate()
print("subprocess 執(zhí)行結(jié)果:out:{} err:{}".format(out, err))
fps_info = str(out, 'utf-8').strip()
if fps_info:
if fps_info.find("/") > 0:
video_fps_str = fps_info.split('/', 1)
fps_result = int(int(video_fps_str[0]) / int(video_fps_str[1]))
else:
fps_result = int(fps_info)
else:
raise Exception('get fps error')
return fps_result
if __name__ == '__main__':
print('視頻的fps為:{}'.format(get_video_fps('D:/tmp/100.mp4')))
代碼說(shuō)明:
1、首先對(duì)視頻格式做了簡(jiǎn)單的判斷,這部分可以按照需求自行調(diào)整。
2、通過(guò)subprocess進(jìn)行命令調(diào)用,獲取命令返回的結(jié)果。注意范圍的結(jié)果為字節(jié)串,需要調(diào)整格式處理。
驗(yàn)證一下
下面是準(zhǔn)備的素材視頻,fps為25,看一下執(zhí)行的結(jié)果。

執(zhí)行結(jié)果

到此這篇關(guān)于Python實(shí)現(xiàn)獲取視頻時(shí)長(zhǎng)功能的文章就介紹到這了,更多相關(guān)Python獲取視頻時(shí)長(zhǎng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python sqlite3事務(wù)處理方法實(shí)例分析
這篇文章主要介紹了Python sqlite3事務(wù)處理方法,結(jié)合具體實(shí)例形式分析了Python針對(duì)sqlite3事務(wù)處理的操作技巧,代碼中包含詳盡的注釋,需要的朋友可以參考下2017-06-06
利用Django模版生成樹狀結(jié)構(gòu)實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于利用Django模版生成樹狀結(jié)構(gòu)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python實(shí)現(xiàn)把多維數(shù)組展開成DataFrame
今天小編就為大家分享一篇Python實(shí)現(xiàn)把多維數(shù)組展開成DataFrame,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
對(duì)Python的zip函數(shù)妙用,旋轉(zhuǎn)矩陣詳解
今天小編就為大家分享一篇對(duì)Python的zip函數(shù)妙用,旋轉(zhuǎn)矩陣詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
python區(qū)塊鏈實(shí)現(xiàn)簡(jiǎn)版工作量證明
這篇文章主要為大家介紹了python區(qū)塊鏈實(shí)現(xiàn)簡(jiǎn)版工作量證明詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
python-itchat 獲取微信群用戶信息的實(shí)例
今天小編就為大家分享一篇python-itchat 獲取微信群用戶信息的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02

