欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)獲取視頻時長功能

 更新時間:2021年12月24日 08:48:22   作者:劍客阿良_ALiang  
這篇文章主要介紹了Python如何實現(xiàn)獲取視頻時長功能,可以精確到毫秒。文中的示例代碼簡潔易懂,對我們的學(xué)習(xí)有一定的幫助,感興趣的可以了解一下

前言

本文提供獲取視頻時長的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)文章

最新評論