Python爬取視頻時長場景實踐示例
簡介:
在視頻相關(guān)測試場景下,例如:有時需要知道全部視頻的匯總時長,顯然一個個打開并且手工計算耗時耗力,我們可以通過編寫腳本進(jìn)行快速匯總。
獲取視頻時長的方式
1、通過subprocess進(jìn)行獲取。
2、通過moviepy庫中VideoFileClip獲取。
3、通過cv2庫獲取。
安裝
1、subprocess:無需安裝,Python內(nèi)置。
2、moviepy:pip install moviepy。
3、cv2:pip install opencv-python
準(zhǔn)備工序:
1、當(dāng)前項目新增videos目錄。
2、you-get 下載幾個視頻。python:超實用下載工具you-get
如:下載了兩個短視頻
獲取視頻時長的3種方式對比
import cv2 import time import subprocess from moviepy.editor import VideoFileClip def video_duration_1(filename): start = time.time() result = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) end = time.time() spend = end - start print("獲取視頻時長方法1耗時:", spend) return float(result.stdout) def video_duration_2(filename): start = time.time() clip = VideoFileClip(filename) end = time.time() spend = end - start print("獲取視頻時長方法2耗時:", spend) return float(clip.duration) def video_duration_3(filename): start = time.time() cap = cv2.VideoCapture(filename) if cap.isOpened(): rate = cap.get(5) frame_num = cap.get(7) duration = frame_num / rate end = time.time() spend = end - start print("獲取視頻時長方法3耗時:", spend) return duration return -1 if __name__ == '__main__': file = r".\videos\mda-mkbhvebqej3cw9yh.mp4" video_time_1 = video_duration_1(file) print(video_time_1) print("*" * 100) video_time_2 = video_duration_2(file) print(video_time_2) print("*" * 100) video_time_3 = video_duration_3(file) print(video_time_3)
執(zhí)行源碼:
結(jié)論:
1、三種方式均可以正常獲取視頻時長,并且準(zhǔn)確。
2、推薦使用cv2獲取視頻時長,耗時最短。
實踐案例:獲取文件夾內(nèi)全部視頻總時長
import cv2 import os def video_duration(dir_name): sum_duration = 0 for root, dirs, files in os.walk(dir_name, topdown=False): for filename in files: cap = cv2.VideoCapture(dir_name + "\\" + filename) if cap.isOpened(): rate = cap.get(5) frame_num = cap.get(7) duration = frame_num / rate sum_duration += duration return sum_duration if __name__ == '__main__': file = r".\videos" total_video_time = video_duration(file) print(f"{file} 目錄下全部視頻總時長為:{total_video_time}秒")
以上就是Python獲取視頻時長場景實踐示例的詳細(xì)內(nèi)容,更多關(guān)于Python獲取視頻時長的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pandas讀取csv格式數(shù)據(jù)時header參數(shù)設(shè)置方法
本文主要介紹了pandas讀取csv格式數(shù)據(jù)時header參數(shù)設(shè)置方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02如何利用python實現(xiàn)windows的批處理及文件夾操作
最近工作中需要幾個腳本運(yùn)行其他程序,幾乎像一個Windows批處理文件,這篇文章主要給大家介紹了關(guān)于如何利用python實現(xiàn)windows的批處理及文件夾操作的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01python+pyhyper實現(xiàn)識別圖片中的車牌號思路詳解
最近領(lǐng)導(dǎo)給布置了一個基于圖片識別車牌號的工具開發(fā)任務(wù),然后就去研究實現(xiàn)邏輯,自己根據(jù)opencv寫了一個小demo,發(fā)現(xiàn)不僅速度慢而且成功率極低。然后,就找到了Hyperlpr開源項目,這篇文章主要介紹了python+pyhyper實現(xiàn)識別圖片中的車牌號,需要的朋友可以參考下2022-12-12Python中的copy()函數(shù)詳解(list,array)
這篇文章主要介紹了Python中的copy()函數(shù)詳解(list,array),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09用Python的Tornado框架結(jié)合memcached頁面改善博客性能
這篇文章主要介紹了用Python的Tornado框架結(jié)合memcached頁面改善vLog性能,主要使用到了緩存來提升性能,需要的朋友可以參考下2015-04-04