Python實現(xiàn)獲取視頻時長功能
前言
本文提供獲取視頻時長的python代碼,精確到毫秒,一如既往的實用主義。
環(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')))
代碼說明:
1、對視頻的后綴格式做了簡單的校驗,如果需要調(diào)整可以自己調(diào)整一下。
2、對輸出的結(jié)果做了處理,輸出int類型的數(shù)據(jù),方便使用。
驗證一下
準備的視頻如下:
驗證一下
補充
Python實現(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')))
代碼說明:
1、首先對視頻格式做了簡單的判斷,這部分可以按照需求自行調(diào)整。
2、通過subprocess進行命令調(diào)用,獲取命令返回的結(jié)果。注意范圍的結(jié)果為字節(jié)串,需要調(diào)整格式處理。
驗證一下
下面是準備的素材視頻,fps為25,看一下執(zhí)行的結(jié)果。
執(zhí)行結(jié)果
到此這篇關(guān)于Python實現(xiàn)獲取視頻時長功能的文章就介紹到這了,更多相關(guān)Python獲取視頻時長內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python的flask接收前臺的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法
這篇文章主要介紹了Python的flask接收前臺的ajax的post數(shù)據(jù)和get數(shù)據(jù)的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04django框架自定義模板標簽(template tag)操作示例
這篇文章主要介紹了django框架自定義模板標簽(template tag)操作,結(jié)合實例形式分析了Django框架自定義模板標簽原理、操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-06-06Python數(shù)據(jù)可視化正態(tài)分布簡單分析及實現(xiàn)代碼
這篇文章主要介紹了Python數(shù)據(jù)可視化正態(tài)分布簡單分析及實現(xiàn)代碼,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12python 實現(xiàn)返回一個列表中出現(xiàn)次數(shù)最多的元素方法
今天小編就為大家分享一篇python 實現(xiàn)返回一個列表中出現(xiàn)次數(shù)最多的元素方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python中try excpet BaseException(異常處理捕獲)的使用
本文主要介紹了Python中try excpet BaseException(異常處理捕獲)的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Pytest使用fixture實現(xiàn)token共享的方法
同學(xué)們在做pytest接口自動化時,會遇到一個場景就是不同的測試用例需要有一個登錄的前置步驟,登錄完成后會獲取到token,用于之后的代碼中,本文給大家介紹Pytest使用fixture實現(xiàn)token共享的方法,感興趣的朋友一起看看吧2023-11-11