skip to content
声控烤箱 | KazooTTT 博客

我自己常用的ffmpeg批处理

Updated:

摘要(由llm生成)

转换FLV格式的视频成HDDP,用于弹幕压制。使用FFmpeg来实现该功能,支持多个操作系统,如Windows和macOS。

720x1080 批量转 1920x1080(两边黑屏)

手机直播的时候录播机录出来的分辨率是 720x1080,使用 ffmpeg 转成横屏的 1920x1080。这样 xml 转 ass 弹幕的时候,就不需要另外处理了,看起来很更舒服。

macos 的写法:

Terminal window
input_folder="" # 要转化的录播的文件夹路径
output_folder="" # 要输出的文件夹路径
# Create the output folder if it does not exist
mkdir -p "$output_folder"
for f in "$input_folder"/*.flv; do
ffmpeg -i "$f" -vf "scale=720:1080,pad=1920:1080:(ow-iw)/2:(oh-ih)/2" -c:a copy "$output_folder/$(basename "${f%.*}.mp4")"
done

windows 的写法:

Terminal window
$input_folder = "Z:\\rec\\48743-hanser\\20240731-又来画画了!" # 要转化的录播的文件夹路径
$output_folder = "Z:\\rec\\48743-hanser\\20240731-又来画画了!" # 要输出的文件夹路径
# Create the output folder if it does not exist
If (-Not (Test-Path $output_folder)) {
New-Item -ItemType Directory -Path $output_folder | Out-Null
}
Get-ChildItem -Path $input_folder -Filter *.flv | ForEach-Object {
$input_file = $_.FullName
$output_file = Join-Path $output_folder ($_.BaseName + ".mp4")
$ffmpeg_args = @("-i", $input_file, "-vf", "scale=720:1080,pad=1920:1080:(ow-iw)/2:(oh-ih)/2", "-c:a", "copy", $output_file)
& ffmpeg $ffmpeg_args
}

效果

image.png

弹幕压制

windows 版 (使用 cuda)

Terminal window
@echo off
set input_folder=YourInputFolderPath
set output_folder=YourOutputFolderPath
for %%a in ("%input_folder%\\*.flv") do (
ffmpeg -hwaccel cuda -c:v h264_cuvid -i "%%a" -vf subtitles="%%~na.ass" -c:v h264_nvenc -b:v 6000k -c:a copy "%output_folder%\\%%~na_压制.mp4" -y
)

macOS 版

#!/bin/bash
input_folder="/path/to/input" # Replace with your input folder path
output_folder="/path/to/output" # Replace with your output folder path
mkdir -p "$output_folder" # Create the output folder if it doesn't exist
for f in "$input_folder"/*.mp4; do
subtitle_file="${f%.*}.ass" # Assumes subtitle file name is same as video file name but with .ass extension
output_file="$output_folder/$(basename "${f%.*}_压制.mp4")" # Output file name with _ass suffix
ffmpeg -i "$f" -vf "ass=$subtitle_file" "$output_file"
done

帧截图

Terminal window
ffmpeg -i inputfilepath %d.jpg

根据视频的帧率来提取截图,每帧都会被提取,并保存为 JPEG 图像文件

Terminal window
ffmpeg -i inputfilepath -vf "fps=1" %d.jpg

每隔 1 秒从视频中提取一帧,并保存为 JPEG 图像文件 ​​​

感谢阅读到这里,期待收到更多的反馈
欢迎关注公众号
kazoottt
公众号二维码