Python获取音视频时长

Python获取音视频时长

Python获取音视频时长

1、安装插件

powershell 复制代码
pip install moviepy -i https://pypi.tuna.tsinghua.edu.cn/simple

2、获取音视频时长.py

上代码:获取音视频时长.py

python 复制代码
# -*- coding: utf-8 -*-
from moviepy.editor import VideoFileClip
# import moviepy.editor.VideoFileClip
import os
import time

def get_time(seconds):
    hour = seconds // 3600  #ok,向下取整
    if len(str(hour)) != 2:
        hour2 = "0" + str(hour)
    else:
        hour2 = str(hour)
    miniute = (seconds - hour * 3600) // 60 #取余
    if len(str(miniute)) != 2:
        miniute2 = "0" + str(miniute)
    else:
        miniute2 = str(miniute)
    second = seconds - hour * 3600 - miniute * 60
    if len(str(second)) == 1:
        second2 = "0" + str(second)
    else:
        second2 = str(second)
    return str(hour2) + ":"+ str(miniute2) + ":" + str(second2)

#得到文件夹下的所有文件
def file_name(file_dir,file_type):
    L=[]
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            # if os.path.splitext(file)[1] == '.wmv':
            if os.path.splitext(file)[1] == file_type:
                L.append(os.path.join(root, file))
    return L

# 得到日期格式
# def get_date_string_file():
#     # return time.strftime("%Y%m%d_%H%M%S",time.localtime())
#     return time.strftime("%Y%m%d", time.localtime())

# 在制定目录创建文件,存在的话就追加写入
def create_text_file(log_file_name,content):
    with open(log_file_name, 'a+') as f:
        f.write(content + '\n')  # 加\n换行显示
        f.close()

# file_dir = "D:/wmv"
# file_type = '.wmv'
file_dir = input("请输入文件目录,例如:'D:/wmv':")
file_type = input("请输入文件扩展名,例如:'.wmv':")
log_file_name = input("请输入日志文件存放目录,例如:'D:/20220115.log':")
# log_file_name = "D:/" + get_date_string_file() + '.txt'
total_times = 0
for file in file_name(file_dir,file_type):
    # print(file)
    # file = "D:/Video_2022-01-08_011427.wmv"
    clip = VideoFileClip(file)
    times = str(clip.duration)
    # times = times.splitlines()[-1].strip()
    # # print(times)
    # # print(int(float(times)))
    file_time = get_time(int(float(times)))
    print(file + " 文件时长:" + file_time)
    create_text_file(log_file_name, file + " 文件时长:" + file_time)
    total_times = total_times + int(float(times))
    # print(file+ " 文件时长:" + str(total_times))
# print("total_times = "+ str(total_times))
print("总时间为:"+get_time(total_times))
create_text_file(log_file_name, "总时间为:"+get_time(total_times))
file_type = input("程序执行完毕,请按任意键退出...")

3、打包exe

pyinstaller -F 获取音视频时长.py

4、下载地址

链接:https://pan.baidu.com/s/1WvsMyPHD3iFsM844gfC2Jg?pwd=yyds

相关推荐
宸津-代码粉碎机1 小时前
OpenAI 连夜迎战 Grok Bot 和 Muse:AI 智能体从 “会聊天” 到 “能办事”,现在入场还来得及吗
java·大数据·人工智能·分布式·python
Y3815326621 小时前
SERP API 计费口径:credits_charged、执行失败扣费与退款边界
python·搜索引擎
chenbingjie_c1 小时前
C++ 进阶核心知识点总结:模板、内存分区、new/delete、内存池
开发语言·c++
Escalating_xu3 小时前
【C 语言】深入理解指针(1·下):指针运算、野指针、assert 与传址实战
java·c语言·开发语言
蒸鱼Yuzheng3 小时前
设备端性能工件可靠导出:断点续传、哈希、manifest 与失败恢复
android·自动化测试·python·adb·数据完整性
实心儿儿3 小时前
Qt — Qt 事件
开发语言·qt
周杰偷奶茶3 小时前
【Java】数据类型与变量
java·开发语言
code斗3 小时前
Java数据结构:堆详解
java·开发语言·数据结构
音视频牛哥3 小时前
四足机器人如何做好智慧电力巡检?从低延迟图传到可信巡检数据闭环
音视频·四足机器人·智慧电力巡检·whip whep·机器人巡检·工业音视频巡检·智能电力巡检技术方案
重生之小比特3 小时前
【C++进阶】map和set
java·开发语言·c++