Skip to content

Instantly share code, notes, and snippets.

@the-eric-kwok
Last active December 15, 2022 09:07
Show Gist options
  • Save the-eric-kwok/e90dfce2fccfc9afab32c89d97ff876b to your computer and use it in GitHub Desktop.
Save the-eric-kwok/e90dfce2fccfc9afab32c89d97ff876b to your computer and use it in GitHub Desktop.
一个用来压制字幕和水印的脚本
# 这个脚本可以用来把ass格式的字幕和logo一起压制到视频里。依赖于ffmpeg和zenity
# 由于使用命令行输入路径的话参数太多,不方便记忆(我自己都记不住),所以使用zenity图形界面来操作,对新手更友好
# logo建议使用png格式,使用透明背景,并且请裁剪成正方形,否则大概会变形
# 设置logo大小指的是logo水印在视频中的大小,默认为200x200的正方形,如果需要压制长方形logo请自行修改源码
$global:position_map = @{
"top_left" = "10:10";
"top_right" = "main_w-overlay_w-10:10";
"bottom_left" = "10:main_h-overlay_h-10";
"bottom_right" = "main_w-overlay_w-10:main_h-overlay_h-10";
}
$global:size = 200; #默认大小为长宽200的正方形
# 如需要压制长方形logo请自行修改下方width和height两个变量
$global:width = $size
$global:height = $size
# 压制主程序
function burn {
param($video, $ass, $logo, $position, $output)
$ass = $ass.Replace("\", "\\\\").Replace(":", "\\:").Replace("'", "\\\'");
ffmpeg -i "$video" -i "$logo" -filter_complex "[1:v]scale=${global:width}:${global:height}[logo];[0:v][logo]overlay=${position}[taged];[taged]ass=${ass}[sub]" -vcodec libx264 -preset veryslow -profile:v high -level:v 4.1 -x264-params keyint=270:min-keyint=30 -b 6000k -maxrate 24000k -r 30 -acodec aac -ac 2 -ab 320k -ar 44100 -y -map '[sub]' -map 0:a "$output"
}
function show_file_selection_dialog {
param($title, $ext)
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ InitialDirectory = [Environment]::GetFolderPath('Desktop') }
$FileBrowser.InitialDirectory = "."
$FileBrowser.Filter = "$ext"
$FileBrowser.title = $title
$FileBrowser.ShowDialog() | Out-Null
return $FileBrowser.FileName
}
# 文件选择
function file_select {
$global:video = show_file_selection_dialog "选择视频文件" "视频文件|*.mkv;*.mp4;*.tv;*.avi;*.rmvb;*.mov;*.qt;*.wmv"
if ([string]::IsNullOrEmpty($global:video)) {
exit
}
$global:ass = show_file_selection_dialog "选择字幕文件" "字幕文件|*.ass"
if ([string]::IsNullOrEmpty($global:ass)) {
exit
}
$global:logo = show_file_selection_dialog "选择水印文件" "水印文件|*.png"
if ([string]::IsNullOrEmpty($global:logo)) {
exit
}
}
# Logo 位置选择
function pos_select {
Write-Output ""
Write-Output "请选择Logo水印位置"
Write-Output "1-右上角(默认)"
Write-Output "2-左上角"
Write-Output "3-右下角"
Write-Output "4-左下角"
Write-Output "请按下对应的按键"
$key = $Host.UI.RawUI.ReadKey();
switch ($key.Character) {
"2" {
Write-Output ""
Write-Output "选择了左上角"
$global:position = $position_map["top_left"]
break;
}
"3" {
Write-Output ""
Write-Output "选择了右下角"
$global:position = $position_map["bottom_right"]
break;
}
"4" {
Write-Output ""
Write-Output "选择了左下角"
$global:position = $position_map["bottom_left"]
break;
}
Default {
Write-Output ""
Write-Output "选择了右上角(默认)"
$global:position = $position_map["top_right"]
}
}
}
# 判断是否安装必要的依赖
where.exe ffmpeg 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Output ""
Write-Output "ffmpeg 未在 PATH 路径中找到,请先安装 ffmpeg 并将其可执行文件配置到 PATH 中!"
exit
}
file_select;
pos_select;
$output = $global:video -replace '\.mkv|\.mp4|\.tv|\.avi|\.rmvb|\.mov|\.qt|\.wmv', '.final.mp4';
if (Test-Path -Path $output -PathType Leaf) {
Write-Output ""
Write-Output "$output 已存在,是否覆盖?"
Write-Output "Y-覆盖"
Write-Output "N-不覆盖(默认)"
Write-Output "请按下对应的按键"
$key = $Host.UI.RawUI.ReadKey();
if ($key.Character -ne 'Y' -and $key.Character -ne 'y') {
exit
}
}
burn $global:video $global:ass $global:logo $global:position $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment