随笔 - 633,  文章 - 0,  评论 - 13,  阅读 - 48万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

代码:

 

复制代码
import subprocess
from datetime import timedelta
import os

def parse_time(time_str):
    """将时间字符串解析为秒"""
    hours, minutes, seconds = map(int, time_str.split(':'))
    return timedelta(hours=hours, minutes=minutes, seconds=seconds).total_seconds()

def ffmpeg_get_duration(input_path):
    """获取视频的总时长(秒)"""
    result = subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', input_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    duration = float(result.stdout.decode('utf-8').strip())
    return duration

def ffmpeg_trim(input_path, output_path, duration):
    """使用ffmpeg裁剪视频的最后3秒"""
    # 计算裁剪的结束时间(视频时长 - 3秒)
    end_time_sec = duration - 3
    start_time_sec = 0

    cmd = [
        'ffmpeg',
        '-i', input_path,
        '-ss', str(start_time_sec),
        '-to', str(end_time_sec),
        '-c', 'copy',
        output_path
    ]

    try:
        subprocess.run(cmd, check=True)
    except subprocess.CalledProcessError as e:
        print(f"An error occurred while processing the video: {e}")

# 指定包含视频文件的文件夹路径
video_folder_path = r'E:\edge下载\81'

# 遍历文件夹中的所有视频文件
for video_file in os.listdir(video_folder_path):
    if video_file.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):  # 根据需要添加或删除文件类型
        # 完整的视频文件路径
        video_path = os.path.join(video_folder_path, video_file)
        # 输出文件路径,这里假设输出文件与原文件同名,但放在不同的文件夹中
        output_folder_path = r'E:\edge下载\81_trimmed'
        if not os.path.exists(output_folder_path):
            os.makedirs(output_folder_path)
        output_path = os.path.join(output_folder_path, video_file)

        # 获取视频的总时长
        original_duration = ffmpeg_get_duration(video_path)

        # 调用函数进行时间裁剪
        ffmpeg_trim(video_path, output_path, original_duration)

        print(f"Trimmed the last 3 seconds of {video_file} successfully.")
复制代码

 

posted on   大话人生  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2022-04-28 人脸训练
2020-04-28 Vue -路由(Vue -Router)
2020-04-28 Vue常用的UI组件-Elment(PC端Vue组件库)(饿了么组件)(推荐)
2020-04-28 Vue常用的UI组件-ant-design-vue
2020-04-28 Vue常用的UI组件-Mint UI(移动端Vue组件库)(饿了么组件)
2020-04-28 Vue常用的UI组件-vant(轻量、可靠的移动端Vue组件库)(推荐)
点击右上角即可分享
微信分享提示