怎么用脚本转换视频帧率

wen 实用脚本 2

本文目录导读:

怎么用脚本转换视频帧率

  1. 使用 FFmpeg 命令行脚本
  2. 批量转换脚本
  3. 高级帧率控制
  4. 配置化转换工具
  5. 使用步骤:

使用 FFmpeg 命令行脚本

FFmpeg 是最强大的视频处理工具,通过脚本可以灵活转换帧率。

Linux/Mac 脚本:

#!/bin/bash
# convert_fps.sh
INPUT=$1
OUTPUT=$2
FPS=$3
# 基础帧率转换
ffmpeg -i "$INPUT" -vf "fps=$FPS" "$OUTPUT"
# 高级选项:保持质量
ffmpeg -i "$INPUT" -vf "fps=$FPS" -c:v libx264 -crf 18 -preset slow "$OUTPUT"
# 音频同步处理
ffmpeg -i "$INPUT" -vf "fps=$FPS" -c:v libx264 -crf 18 -c:a copy "$OUTPUT"

Windows 批处理脚本:

@echo off
REM convert_fps.bat
set INPUT=%1
set OUTPUT=%2
set FPS=%3
ffmpeg -i "%INPUT%" -vf "fps=%FPS%" "%OUTPUT%"
echo 转换完成!

批量转换脚本

Python 批量处理脚本:

#!/usr/bin/env python3
# batch_fps_convert.py
import os
import subprocess
def convert_fps(input_file, output_file, fps):
    """转换单个视频帧率"""
    cmd = [
        'ffmpeg',
        '-i', input_file,
        '-vf', f'fps={fps}',
        '-c:v', 'libx264',
        '-crf', '18',
        '-c:a', 'copy',
        '-y',  # 覆盖已有文件
        output_file
    ]
    subprocess.run(cmd, check=True)
def batch_convert(directory, output_fps):
    """批量转换目录中所有视频"""
    for filename in os.listdir(directory):
        if filename.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):
            input_path = os.path.join(directory, filename)
            output_path = os.path.join(directory, f'{os.path.splitext(filename)[0]}_{output_fps}fps.mp4')
            print(f"正在转换: {filename}")
            try:
                convert_fps(input_path, output_path, output_fps)
                print(f"完成: {output_path}")
            except Exception as e:
                print(f"转换失败 {filename}: {e}")
if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description='批量转换视频帧率')
    parser.add_argument('directory', help='视频所在目录')
    parser.add_argument('fps', type=int, help='目标帧率')
    args = parser.parse_args()
    batch_convert(args.directory, args.fps)

高级帧率控制

智能帧率转换脚本:

#!/bin/bash
# smart_fps_convert.sh
input_file=$1
target_fps=$2
mode=${3:-"standard"}  # standard, motion_blend, minterpolate
echo "输入文件: $input_file"
echo "目标帧率: $target_fps"
case $mode in
    "standard")
        # 标准帧率转换(丢帧或复制帧)
        ffmpeg -i "$input_file" -vf "fps=$target_fps" -c:v libx264 -crf 18 "output_${target_fps}fps.mp4"
        ;;
    "motion_blend")
        # 运动模糊帧率转换
        ffmpeg -i "$input_file" -vf "fps=$target_fps,blend=all_mode=addition" -c:v libx264 -crf 18 "output_blended_${target_fps}fps.mp4"
        ;;
    "minterpolate")
        # 智能插帧(平滑运动)
        ffmpeg -i "$input_file" -vf "minterpolate=fps=$target_fps:mi_mode=mci:mc_mode=aobmc:me_mode=bidir" -c:v libx264 -crf 18 "output_interpolated_${target_fps}fps.mp4"
        ;;
    *)
        echo "错误模式"
        exit 1
        ;;
esac
echo "转换完成!"

配置化转换工具

Python 配置文件方式:

# fps_converter.py
import subprocess
import json
class VideoConverter:
    def __init__(self, config_file):
        with open(config_file, 'r') as f:
            self.config = json.load(f)
    def convert(self):
        for video in self.config['videos']:
            input_file = video['input']
            output_file = video['output']
            fps = video['fps']
            # 检查是否使用高质量模式
            if self.config.get('high_quality', False):
                cmd = f'ffmpeg -i {input_file} -vf fps={fps} -c:v libx264 -crf 18 -preset slow {output_file}'
            else:
                cmd = f'ffmpeg -i {input_file} -vf fps={fps} {output_file}'
            subprocess.run(cmd, shell=True)
            print(f"转换完成: {output_file}")
# 使用示例
if __name__ == "__main__":
    converter = VideoConverter('config.json')
    converter.convert()

配置文件 config.json

{
    "high_quality": true,
    "videos": [
        {
            "input": "input1.mp4",
            "output": "output1_30fps.mp4",
            "fps": 30
        },
        {
            "input": "input2.mp4",
            "output": "output2_60fps.mp4",
            "fps": 60
        }
    ]
}

使用步骤:

  1. 安装 FFmpeg(必需):

    # macOS
    brew install ffmpeg
    # Ubuntu/Debian
    sudo apt install ffmpeg
    # Windows
    # 下载 ffmpeg.exe 并添加到 PATH
  2. 赋予脚本执行权限

    chmod +x convert_fps.sh
    ./convert_fps.sh input.mp4 output.mp4 30
  3. 常见帧率转换需求

    # 30fps → 60fps
    ffmpeg -i input.mp4 -vf "fps=60" output_60fps.mp4
    # 60fps → 30fps
    ffmpeg -i input.mp4 -vf "fps=30" output_30fps.mp4
    # 24fps → 30fps(运动插值)
    ffmpeg -i input.mp4 -vf "minterpolate=fps=30:mi_mode=blend" output_30fps.mp4

这些脚本可以根据你的具体需求进行修改和扩展,支持批量处理、不同编码质量设置等,需要我解释某个特定部分吗?

抱歉,评论功能暂时关闭!