首页 > 编程语言 >python从时间上裁切视频

python从时间上裁切视频

时间:2024-04-20 21:13:31浏览次数:29  
标签:视频 end ffmpeg 裁切 output python start time path

代码

import subprocess
from datetime import timedelta

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_trim(input_path, output_path, start_time, end_time):
    # 将时间字符串转换为秒
    start_time_sec = parse_time(start_time)
    end_time_sec = parse_time(end_time)

    # 构建ffmpeg命令
    cmd = [
        'ffmpeg',
        '-i', input_path,          # 输入文件
        '-ss', str(start_time_sec),  # 开始时间
        '-to', str(end_time_sec),    # 结束时间
        '-c', 'copy',               # 复制编解码器设置,避免重新编码
        output_path                 # 输出文件
    ]

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

# 要裁剪的视频文件路径
video_path = r'E:\edge下载\80-\80-难度等级_keyong.mp4'
# 裁剪后的视频文件路径
output_path = r'E:\edge下载\80-\80-难度等级_qutou.mp4'
# 开始时间(HH:MM:SS格式)
start_time = '00:00:08'  # 视频的第8秒开始
# 结束时间(HH:MM:SS格式)
end_time = '00:03:38'    # 视频的第3分37秒结束

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

 

标签:视频,end,ffmpeg,裁切,output,python,start,time,path
From: https://www.cnblogs.com/jingzaixin/p/18148170

相关文章

  • python从大小上裁切视频
    1.代码1importsubprocessdefffmpeg_crop(input_path,output_path,width,height,x,y):#构建ffmpeg命令cmd=['ffmpeg','-i',input_path,'-filter:v',f'crop={width}:{height}:{x}:{y}'......
  • 解决 macOS 下 Python 3.8 安装 mysqlclient 的问题
    环境背景Python版本:3.8macOS版本:14.4(M2芯片)在安装mysqlclient时遇到的问题我在网上找到的方案基本上都是通过brewinstallmysql-connector-c安装、修改mysql_config文件、安装openssl及gcc,这个解决方案对我并没有效果解决方案步骤一:配置环境变量#使用pkg-config......
  • Python Unittest
    1fromunittestimportTestCase2fromtriangleimportarea_of_a_triangle34classTestAreaOfTriangle(TestCase):56deftest_float_values(self):7"""Testareaswhenvaluesarefloats"""8se......
  • 回归问题求解 python---梯度下降+最小二乘法
      MSE=1/m*∑i=1m(yi−y^i)2 a=[1.,2.,3.,4.,5.,6.,7.,8.,9.]b=[3.,5.,7.,9.,11.,13.,15.,17.,19.]points=[[a[i],b[i]]foriinrange(len(a))]lr=0.001eps=0.0001m=len(......
  • C# ffmpeg m3u8 ts 视频拼接mp4
    准备拼接文件里面放的是需要拼接视频文件的路径concat_list.txt  准备代码 privatevoidCombineFile(stringfilesDir,stringtargetmp4fileName){stringbasedir=AppDomain.CurrentDomain.BaseDirectory;//ffmpeg的......
  • C# 解密m3u8 ts视频文件为mp4
    代码:try{//读取的加密视频ts文件路径byte[]encodeBuffer=File.ReadAllBytes("C:\\Users\\admin\\Downloads\\322251.ts");///A216DF0DA0082028163781ECC258BA5B代表winhex看到的字符串32734893fb097a767c9ea903936a6d8b代表m3u8文件中的iv偏移......
  • Python量化交易系统实战_Python常用库介绍
    作者:麦克煎蛋  出处:https://www.cnblogs.com/mazhiyong/转载请保留这段声明,谢谢! 在量化开发过程中,有一些强大的库是必须学会使用和熟悉的,这里简单整理下。一、Pandas1、简介Pandas是Python语言的一个扩展程序库,用于数据分析。Pandas名字衍生自术语"paneldata"(面板......
  • 【Python】安装配置gym
    gym是python中的一个强化学习环境,想要完整配置并跑起来坑还是比较多的。下面记录一下Windows完整安装过程,Linux下过程基本类似。1.执行pipinstallgym直接安装的是0.26.2版本,网上常见的代码无法兼容,这里安装0.25.2版,并且安装对应的pygame。执行:pipinstallgym==0.25.2pip......
  • Python与Java数据结构语法区别
    数组参考链接:CS61BPythonzeroedLst=[0,0,0]lst=[4,7,10]lst[0]=5print(lst[0])print(lst)print(len(lst))Javaint[]zeroedArray=newint[3];int[]array={4,7,10};array[0]=5;System.out.println(array[0]);System.out.println(Ar......
  • Python量化交易系统实战_实现股票实盘交易
    作者:麦克煎蛋  出处:https://www.cnblogs.com/mazhiyong/转载请保留这段声明,谢谢!这里以EasyTrader为例,感受下如何实现程序化交易。一、EasyTrader技术原理简介EasyTrader主要原理是利用pywinauto自动获取同花顺上相应控件的值,进行模拟自动化的操作,它给散户提供了一个强大......