无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站投放广告、加入VIP会员,请联系 微信:wuyouceo
查看: 219|回复: 18
打印 上一主题 下一主题

[原创] 用ai写的批量视频快速无损旋转脚本[python]

[复制链接]
跳转到指定楼层
1#
发表于 7 小时前 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 cpanel 于 2025-12-14 03:26 编辑

   脚本是基于ffmpeg的,
需要与ffmpeg.exe同目录

   因为有很多视频存在拍摄方向不一样的问题,又一直找不到合适的软件,
所以就找AI开发了这个脚本。
优点,可以批量多线程无损快速旋转视频[平均几秒钟一个视频]。
缺点,只支持部分编码视频,视频格式也只支持mp4mpv等常用格式,
当然格式可以自行往代码中添加,但是有些编码是无法用这种方式旋转的。
若有大佬帮忙改进,请把完善后的代码发我一份,谢谢。

下面是代码:
  
  1. import os
  2. import subprocess
  3. from tkinter import Tk, filedialog, simpledialog, messagebox

  4. # 支持的视频扩展名
  5. SUPPORTED_EXTENSIONS = ('.mp4', '.mov', '.m4v', '.mkv')

  6. def select_directory(title):
  7.     """弹出窗口选择目录"""
  8.     root = Tk()
  9.     root.withdraw()
  10.     folder = filedialog.askdirectory(title=title)
  11.     return folder

  12. def get_user_input(prompt, default_value, value_type=str, allowed_values=None):
  13.     """弹出窗口获取用户输入"""
  14.     root = Tk()
  15.     root.withdraw()
  16.     while True:
  17.         try:
  18.             value = simpledialog.askstring("输入参数", prompt + f"(默认:{default_value})")
  19.             if value is None or value.strip() == "":
  20.                 return default_value
  21.             typed_value = value_type(value)
  22.             if allowed_values and typed_value not in allowed_values:
  23.                 raise ValueError()
  24.             return typed_value
  25.         except ValueError:
  26.             messagebox.showerror("输入错误", f"请输入有效的值(允许: {allowed_values})")

  27. def rotate_video(input_path, output_path, angle, threads):
  28.     """调用 ffmpeg 设置 display_rotation,无损旋转"""
  29.     cmd = [
  30.         'ffmpeg',
  31.         '-threads', str(threads),
  32.         '-display_rotation:0', str(angle),
  33.         '-i', input_path,
  34.         '-c', 'copy',
  35.         output_path
  36.     ]
  37.     try:
  38.         print(f"正在处理: {os.path.basename(input_path)}")
  39.         subprocess.run(cmd, check=True)
  40.         print(f"完成: {output_path}")
  41.     except subprocess.CalledProcessError as e:
  42.         print(f"处理失败: {input_path}")
  43.         print(e)

  44. def process_videos(input_dir, output_dir, angle, threads):
  45.     """处理所有视频文件"""
  46.     for filename in os.listdir(input_dir):
  47.         if filename.lower().endswith(SUPPORTED_EXTENSIONS):
  48.             input_path = os.path.join(input_dir, filename)
  49.             output_path = os.path.join(output_dir, filename)
  50.             rotate_video(input_path, output_path, angle, threads)

  51. if __name__ == "__main__":
  52.     print("请选择输入目录:")
  53.     input_folder = select_directory("选择包含视频的输入文件夹")
  54.     if not input_folder:
  55.         print("未选择输入目录,程序退出。")
  56.         exit()

  57.     print("请选择输出目录:")
  58.     output_folder = select_directory("选择用于保存视频的输出文件夹")
  59.     if not output_folder:
  60.         print("未选择输出目录,程序退出。")
  61.         exit()

  62.     # 获取用户输入的旋转角度(符合人类直觉:顺时针旋转)
  63.     user_rotation = get_user_input(
  64.         "请输入顺时针旋转角度(可选:0, 90, 180, 270)",
  65.         default_value=90,
  66.         value_type=int,
  67.         allowed_values=[0, 90, 180, 270]
  68.     )

  69.     # 映射为 FFmpeg 所需的角度(绕 Y 轴顺时针 → 逆时针角度)
  70.     angle_map = {
  71.         0: 0,
  72.         90: 270,
  73.         180: 180,
  74.         270: 90
  75.     }
  76.     rotation_angle = angle_map[user_rotation]

  77.     # 获取线程数
  78.     threads = get_user_input(
  79.         "请输入要使用的 CPU 线程数(建议 1-8)",
  80.         default_value=2,
  81.         value_type=int
  82.     )

  83.     print(f"\n开始处理视频...\n输入目录: {input_folder}\n输出目录: {output_folder}\n旋转角度: {user_rotation}°(实际写入 {rotation_angle}°)\n线程数: {threads}\n")
  84.     process_videos(input_folder, output_folder, rotation_angle, threads)
  85.     print("全部处理完成!")
复制代码




2#
发表于 3 小时前 | 只看该作者
谢谢楼主分享
回复

使用道具 举报

3#
发表于 3 小时前 | 只看该作者
感谢您的分享!
回复

使用道具 举报

4#
发表于 3 小时前 | 只看该作者
很不错,很nice
回复

使用道具 举报

5#
发表于 3 小时前 | 只看该作者
感谢分享
回复

使用道具 举报

6#
发表于 3 小时前 | 只看该作者
谢谢分享
回复

使用道具 举报

7#
发表于 2 小时前 | 只看该作者
感谢楼主分享.
回复

使用道具 举报

8#
发表于 2 小时前 | 只看该作者
谢谢分享
回复

使用道具 举报

9#
发表于 2 小时前 | 只看该作者
没有Python
回复

使用道具 举报

10#
发表于 2 小时前 | 只看该作者
学习一下
回复

使用道具 举报

11#
发表于 1 小时前 | 只看该作者
谢谢楼主分享
回复

使用道具 举报

12#
发表于 1 小时前 | 只看该作者
驚為天人的神作,看了讓人熱血沸騰,感謝分享。
回复

使用道具 举报

13#
发表于 1 小时前 | 只看该作者
谢谢楼主分享
回复

使用道具 举报

14#
发表于 1 小时前 | 只看该作者
感谢分享!
回复

使用道具 举报

15#
发表于 1 小时前 | 只看该作者
谢谢楼主分享
回复

使用道具 举报

16#
发表于 1 小时前 | 只看该作者
支持~
回复

使用道具 举报

17#
发表于 半小时前 | 只看该作者
感谢分享!
回复

使用道具 举报

18#
发表于 半小时前 | 只看该作者
支持原创
回复

使用道具 举报

19#
发表于 5 分钟前 | 只看该作者
感谢开源
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2025-12-14 10:26

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表