Python爬取視頻時(shí)長(zhǎng)場(chǎng)景實(shí)踐示例

目錄

簡(jiǎn)介:

在視頻相關(guān)測(cè)試場(chǎng)景下,例如:有時(shí)需要知道全部視頻得匯總時(shí)長(zhǎng),顯然一個(gè)個(gè)打開并且手工計(jì)算耗時(shí)耗力,我們可以通過編寫進(jìn)行快速匯總。

獲取視頻時(shí)長(zhǎng)得方式

1、通過subprocess進(jìn)行獲取。

2、通過moviepy庫(kù)中VideoFileClip獲取。

3、通過cv2庫(kù)獲取。

安裝

1、subprocess:無(wú)需安裝,Python內(nèi)置。

2、moviepy:pip install moviepy。

3、cv2:pip install opencv-python

準(zhǔn)備工序:

1、當(dāng)前項(xiàng)目新增videos目錄。

2、you-get 下載幾個(gè)視頻。python:超實(shí)用下載工具you-get

如:下載了兩個(gè)短視頻

獲取視頻時(shí)長(zhǎng)得3種方式對(duì)比

import cv2import timeimport subprocessfrom moviepy.editor import VideoFileClipdef 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("獲取視頻時(shí)長(zhǎng)方法1耗時(shí):", spend)    return float(result.stdout)def video_duration_2(filename):    start = time.time()    clip = VideoFileClip(filename)    end = time.time()    spend = end - start    print("獲取視頻時(shí)長(zhǎng)方法2耗時(shí):", 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("獲取視頻時(shí)長(zhǎng)方法3耗時(shí):", spend)        return duration    return -1if __name__ == '__main__':    file = r".videosmda-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、三種方式均可以正常獲取視頻時(shí)長(zhǎng),并且準(zhǔn)確。

2、推薦使用cv2獲取視頻時(shí)長(zhǎng),耗時(shí)最短。

實(shí)踐案例:獲取文件夾內(nèi)全部視頻總時(shí)長(zhǎng)

import cv2import osdef 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_durationif __name__ == '__main__':    file = r".videos"    total_video_time = video_duration(file)    print(f"{file} 目錄下全部視頻總時(shí)長(zhǎng)為:{total_video_time}秒")

以上就是Python獲取視頻時(shí)長(zhǎng)場(chǎng)景實(shí)踐示例得詳細(xì)內(nèi)容,更多關(guān)于Python獲取視頻時(shí)長(zhǎng)得資料請(qǐng)關(guān)注之家其它相關(guān)內(nèi)容!

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評(píng)論
更多 網(wǎng)友評(píng)論1 條評(píng)論)
暫無(wú)評(píng)論

返回頂部

主站蜘蛛池模板: 里番acg全彩本子同人视频| 最近最新中文字幕完整版免费高清| 一级毛片大全免费播放下载| 国产午夜精品一区二区三区不卡| 欧美交性a视频免费| 2022国产精品视频| 亚洲成在人线在线播放无码| 草草影院www色欧美极品| 亚洲精品无码久久毛片| 在线观看免费黄色网址| 波多野结大战三个黑鬼| 99久久精品免费观看国产| 亚洲精品国产精品乱码视色| 国语做受对白xxxxx在线| 欧美日韩视频在线播放| 两个人看的视频高清在线www| 亚洲免费色视频| 国产大学生粉嫩无套流白浆| 日本一道高清一区二区三区| 视频在线观看一区| 一本久久a久久精品vr综合| 人妻少妇精品久久久久久| 国产精品国产三级在线专区| 樱桃视频影院在线播放| 边摸边吃奶边做爽免费视频网站| 久久91这里精品国产2020| 免费精品国产自产拍观看| 国产精品资源在线观看| 日韩电影在线看| 精品国产福利在线观看91啪| 99久久综合久中文字幕| 久青草国产97香蕉在线视频| 国产99热在线观看| 渣男渣女抹胸渣男渣女app| 99re在线播放视频| 久久国产精品免费一区二区三区| 午夜男女爽爽影院网站| 国产精品无码久久av| 无翼乌全彩无遮挡动漫视频 | 忘忧草社区中文字幕| 欧美日韩在线不卡|