本文目录导读:

我来介绍几种批量转码的脚本方法,主要针对视频和音频文件。
使用 FFmpeg + Shell 脚本(最常用)
视频批量转码
#!/bin/bash
# 批量将 MP4 转为 H.264 + AAC 格式
# 创建输出目录
mkdir -p output
# 遍历所有 .mp4 文件
for file in *.mp4; do
if [ -f "$file" ]; then
echo "正在转换: $file"
ffmpeg -i "$file" \
-c:v libx264 -preset medium -crf 23 \
-c:a aac -b:a 128k \
-movflags +faststart \
"output/${file%.mp4}_converted.mp4"
fi
done
echo "全部转换完成!"
批量转换不同格式
#!/bin/bash
# 将 MKV 转为 MP4,保留原始视频和音频质量
mkdir -p mp4_output
for file in *.mkv; do
if [ -f "$file" ]; then
echo "处理: $file"
ffmpeg -i "$file" -c copy \
"mp4_output/${file%.mkv}.mp4"
fi
done
echo "完成!"
音频批量转码
#!/bin/bash
# 批量将 WAV 转为 MP3
mkdir -p mp3_output
for file in *.wav; do
if [ -f "$file" ]; then
echo "转换: $file"
ffmpeg -i "$file" \
-codec:a libmp3lame -qscale:a 2 \
"mp3_output/${file%.wav}.mp3"
fi
done
echo "所有音频已转换!"
Python 脚本(更灵活的批量处理)
#!/usr/bin/env python3
import os
import subprocess
from pathlib import Path
def batch_convert():
# 设置输入输出目录
input_dir = Path("input_videos")
output_dir = Path("output_videos")
output_dir.mkdir(exist_ok=True)
# 支持的输入格式
input_formats = ['.mp4', '.avi', '.mov', '.mkv']
# 遍历所有视频文件
for file in input_dir.iterdir():
if file.suffix.lower() in input_formats:
print(f"正在转换: {file.name}")
# 输出文件路径
output_file = output_dir / f"{file.stem}_converted.mp4"
# FFmpeg 命令
cmd = [
'ffmpeg',
'-i', str(file),
'-c:v', 'libx264',
'-preset', 'fast',
'-crf', '23',
'-c:a', 'aac',
'-b:a', '128k',
'-y', # 覆盖已存在的文件
str(output_file)
]
# 执行命令
try:
subprocess.run(cmd, check=True)
print(f"✓ 完成: {output_file.name}")
except subprocess.CalledProcessError as e:
print(f"✗ 转换失败: {file.name} - {e}")
if __name__ == "__main__":
batch_convert()
带进度显示的增强脚本
#!/bin/bash
# 带进度条的高级批量转换脚本
convert_with_progress() {
input_file="$1"
output_file="$2"
# 获取视频时长(秒)
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$input_file")
duration=${duration%.*}
echo "转换: $(basename "$input_file") (${duration}s)"
# 使用 ffmpeg 的 progress 参数
ffmpeg -i "$input_file" \
-c:v libx264 -preset medium -crf 23 \
-c:a aac \
-progress pipe:1 \
"$output_file" 2>&1 | while read line; do
if [[ "$line" == out_time_ms=* ]]; then
current_ms="${line#out_time_ms=}"
current_s=$((current_ms / 1000000))
if [ "$duration" -gt 0 ]; then
percent=$((current_s * 100 / duration))
echo -ne "\r进度: ${percent}%"
fi
fi
done
echo -e "\r转换完成: $output_file"
}
# 主循环
mkdir -p output
for file in *.mp4 *.avi *.mkv; do
[ -f "$file" ] || continue
convert_with_progress "$file" "output/${file%.*}_converted.mp4"
done
并行批量转换(提升速度)
#!/bin/bash
# 使用 xargs 并行处理
mkdir -p output
export -f convert_one
convert_one() {
file="$1"
echo "处理: $file"
ffmpeg -i "$file" \
-c:v libx264 -preset fast -crf 23 \
-c:a aac -b:a 128k \
"output/${file%.*}.mp4"
}
# 使用 4 个并行任务
ls *.mp4 | xargs -P 4 -I {} bash -c 'convert_one "$@"' _ {}
Windows PowerShell 版本
# 批量转换视频格式
$inputDir = ".\videos"
$outputDir = ".\converted"
New-Item -ItemType Directory -Force -Path $outputDir
Get-ChildItem -Path $inputDir -Include *.mp4,*.avi -Recurse | ForEach-Object {
Write-Host "Converting: $($_.Name)"
$outputFile = Join-Path $outputDir "$($_.BaseName)_converted.mp4"
& ffmpeg -i $_.FullName `
-c:v libx264 -preset medium -crf 23 `
-c:a aac -b:a 128k `
$outputFile
Write-Host "✓ Done: $outputFile" -ForegroundColor Green
}
使用说明:
-
安装 FFmpeg:
- Linux:
sudo apt install ffmpeg - macOS:
brew install ffmpeg - Windows: 下载 FFmpeg 并添加环境变量
- Linux:
-
使用方法:
# 保存脚本并赋予执行权限 chmod +x convert.sh ./convert.sh
-
自定义参数:
-crf: 质量(18-28,越小质量越高)-preset: 速度优先级(ultrafast到veryslow)-b:a: 音频码率-s: 分辨率(如 1920x1080)
根据你的具体需求(如转成什么格式、压缩比例等),可以调整相应的参数,需要更具体的帮助吗?