首页 > 编程语言 >python将竖屏视频转为横屏

python将竖屏视频转为横屏

时间:2024-05-14 17:22:19浏览次数:11  
标签:ffmpeg python 竖屏 process 横屏 progress output path 旋转

python3.6版本代码:

import subprocess
import re
from threading import Thread

def print_ffmpeg_progress(process):
    """
    从ffmpeg进程中读取并打印进度信息
    """
    pattern = re.compile(r'frame=\s+(\d+)')  # 正则表达式,用于匹配帧数
    for line in iter(process.stderr.readline, ''):
        if 'progress' in line:
            match = pattern.search(line)
            if match:
                frame = int(match.group(1))
                print(f"Processing frame: {frame}")

def rotate_video_with_ffmpeg(input_path, output_path):
    # 构建ffmpeg命令
    command = [
        'ffmpeg',
        '-i', input_path,
        '-vf', 'transpose=2',  # 顺时针旋转90度
        '-c:a', 'copy',
        output_path,
        '-loglevel', 'error'  # 设置日志级别为error以获取更多错误信息
    ]

    # 启动ffmpeg进程
    with subprocess.Popen(command, stderr=subprocess.PIPE, universal_newlines=True) as process:
        # 创建并启动一个新线程来打印进度
        progress_thread = Thread(target=print_ffmpeg_progress, args=(process,))
        progress_thread.daemon = True
        progress_thread.start()

        # 等待ffmpeg进程结束
        process.wait()

        # 读取 FFmpeg 进程的错误输出
        error_output, _ = process.communicate()

        # 确保进度线程结束
        progress_thread.join()

        # 检查ffmpeg进程的状态
        if process.returncode != 0:
            print("FFmpeg process failed with error code:", process.returncode)
            print("Error output:\n", error_output)
        else:
            print("Video rotation completed successfully.")

# 使用示例
input_video_path = "E:\\pian\\fanchang.mp4"
output_video_path = "E:\\pian\\fanchang_旋转90.mp4"
rotate_video_with_ffmpeg(input_video_path, output_video_path)


# """
#
# ffmpeg中的transpose过滤器接受的参数值与旋转角度的表示方式不同。在ffmpeg中,transpose过滤器的参数值用来指定旋转的方向,而不是旋转的角度。参数值与旋转方向的对应关系如下:
#
# 0:不旋转(默认)
# 1:逆时针旋转90度
# 2:顺时针旋转90度
# 3:旋转180度
# 所以,如果你想要顺时针旋转90度(将竖屏视频转换为横屏),应该使用transpose=2。逆时针旋转90度则使用transpose=1。
#
# 根据你提供的错误信息,你已经尝试使用transpose=90,这不是一个有效的参数值,因为transpose过滤器不接受角度值,只接受上述的旋转方向值。
# """

 

标签:ffmpeg,python,竖屏,process,横屏,progress,output,path,旋转
From: https://www.cnblogs.com/jingzaixin/p/18191762

相关文章

  • Python 常用第三方库 urllib3使用
    urllib3概述线程安全连接池管理客户端SSL/TLS验证支持HTTP和SOCKS代理官方文档:urllib32.0.4documentationurllib3安装通过pip安装pipinstallurllib3urllib3发送HTTP请求导入urllib3模块创建PoolManager实例调用request()方法importur......
  • Python实现简易版选课系统
    需求:一、创建学生类#创建学生类importrandomclassStudent:def__init__(self,num,name,address,course_lst=None):self.num=numself.name=nameself.address=addressifcourse_lst:self.cous_lst=cous_lst......
  • python列表中切片的正负数
    先说结论:列表切片的格式为[start:end:step]其中step代表步长,即每从start位置开始每隔几个元素取一个值step为正数时表示切片取值方向为:从左往右;为负数时:从右往左start,end代表切片取值的起始和结束位置,请注意这个词:位置,我们假设可以取值的范围是数学中x坐标轴......
  • Python如何访问闭包中的变量
    你想要扩展函数中的某个闭包,允许它能访问和修改函数的内部变量。解决方案通常,闭包的内部变量对外界是完全隐藏的。但可以编写访问函数,将其作为函数属性绑定到闭包上来实现访问。defsample():n=0#闭包函数deffunc():print('n=',n)#属性n的......
  • Python中如何避免字典和元组的多重嵌套的方法
    一、字典、元组的多重嵌套例1:记录全班学生的成绩。分析:定义一个SimpleGradebook类,学生名是字典self._grades的键,成绩是字典self._grades的值。classSimpleGradebook():def__init__(self):self._grades={}defadd_student(self,name):self.......
  • python类函数定义第一个参数必须是self
     如果不写self,则会报错   加上之后错误就会消失 ......
  • Python 内置正则表达式库re的使用
    什么是正则表达式正则表达式就是记录文本规则的代码可以查找操作符合某些复杂规则的字符串使用场景处理字符串处理日志在python中使用正则表达式把正则表达式作为模式字符串正则表达式可以使用原生字符串来表示原生字符串需要在字符串前方加上r'string'#匹配......
  • 爬虫 python的第一天
    1、安装pycharm及python相关的安装2、新建python项目3、引包 importrequestspipinstallrequests4、打开网页,找打你想要爬的数据URL 5、写代码获取到对应的数据,保存到本地。importrequestsurl="https://sns-video-al.xhscdn.com/stream/110/259/01e640315b0ef......
  • Python 内置库 多线程threading使用讲解
    线程基本使用单线程defmain():print("在扔一个苹果")if__name__=="__main__":main()多线程Python提供了thread、threading等模块来进行线程的创建与管理,后者在线程管理能力上更进一步,因此我们通常使用threading模块。创建一个线程需要指定该线程执行的任务(函......
  • Python 中寻找列表最大值位置的方法
    前言在Python编程中,经常需要对列表进行操作,其中一个常见的任务是寻找列表中的最大值以及其所在的位置。本文将介绍几种方法来实现这个任务。方法一:使用内置函数max()和index()Python提供了内置函数max()来找到列表中的最大值,同时可以使用index()方法找到该最大值在......