使用python获取视频时长/并将秒转化为时分秒的格式

获取当前py文件同级目录以及子目录下的所有视频的时长(此处代码写的是mp4,其他格式请将mp4改为其他格式后自测),把下面这长代码保存为py后,放到你的视频的目录下执行即可输出视频的时长。

 

废话少数说,上码:

# -*- coding: utf-8 -*-

def time_convert(seconds):
    """
        将秒换成合适的时间,如果超过一分钟就换算成"分钟:秒",如果是小时,就换算成"小时:分钟:秒"单位换算
    """
    print(f'时间换算{seconds}')
    M,H = 60,3600
    if seconds < M:
        return f'00:00:0{seconds}' if seconds < 10 else f'00:00:{str(seconds)}'
    elif seconds < H:
        _M = int(seconds/M)
        _S = int(seconds%M)
        return f'00:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'
    else:
        _H = int(seconds/H)
        _M = int(seconds%H/M)
        _S = int(seconds%H%M)
        return f'{f"0{_H}" if _H < 10 else str(_H)}:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'


def get_video_times(video_path):
    """
    pip install moviepy
    获取指定的视频时长
    
    """
    from moviepy.editor import VideoFileClip
    video_clip = VideoFileClip(video_path)
    durantion = video_clip.duration
    video_clip.reader.close()
    video_clip.audio.reader.close_proc()
    return durantion

def get_current_path():
    """
    获取当前文件所在的目录/获取当前路径
    """
    import os
    return os.path.abspath(os.path.dirname(__file__))  # 获取当前py文件所在文件夹的路径


def get_video_duration(root_path):
    """
    获取指定目录下以及目录下的所有子目录内的mp4文件的视频时长
    """
    import os
    
    for root, dirs, files in os.walk(root_path):
        # 递归遍历当前py目录下的所有目录及文件,比如遍历到/a/aa/aaa/aaa.txt,则root='/a/aa/aaa/',dir=root路径下的所有文件夹名称,dir=root路径下的所有文件的名称
        # print(root)   # 当前所在路径
        # print(dirs)   # 当前所在路径下的所有目录名
        # print(files)   # 当前所在路径下的所有文件名
        for file_name in files:
            if file_name.endswith('.mp4'):
                duration = time_convert(get_video_times(os.path.join(root,file_name)))
                print(f'{file_name} - {duration}')


def run():
    import os
    path = os.path.abspath(os.path.dirname(__file__)) # 获取当前py文件所在目录的路径
    get_video_duration(path)


if __name__ == "__main__":
    run()
    

 

效果图:

 

其中最主要代码是获取视频时长的这一段:

def get_video_times(video_path):
    """
    pip install moviepy
    获取指定的视频时长,单位是秒
    
    """
    from moviepy.editor import VideoFileClip
    video_clip = VideoFileClip(video_path)
    durantion = video_clip.duration
    video_clip.reader.close()
    video_clip.audio.reader.close_proc()
    return durantion

 

然后再根据获取到的秒数转换成HH:mm:ss:

def time_convert(seconds):
    """
        将秒换成合适的时间,如果超过一分钟就换算成"分钟:秒",如果是小时,就换算成"小时:分钟:秒"单位换算
    """
    print(f'时间换算{seconds}')
    M,H = 60,3600
    if seconds < M:
        return f'00:00:0{seconds}' if seconds < 10 else f'00:00:{str(seconds)}'
    elif seconds < H:
        _M = int(seconds/M)
        _S = int(seconds%M)
        return f'00:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'
    else:
        _H = int(seconds/H)
        _M = int(seconds%H/M)
        _S = int(seconds%H%M)
        return f'{f"0{_H}" if _H < 10 else str(_H)}:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值