Skip to content

Instantly share code, notes, and snippets.

@Philmist
Last active October 24, 2024 07:20
Show Gist options
  • Save Philmist/3a0229d567de85fb72951cf8c4b64190 to your computer and use it in GitHub Desktop.
Save Philmist/3a0229d567de85fb72951cf8c4b64190 to your computer and use it in GitHub Desktop.
地獄のようなffmpegクロスフェード再び
<#
地獄のようなffmpesクロスフェード再び
ポイント:
* xfadeはfpsを合わせないと文句を言うので指定しておく
* xfadeはオフセットと移行時間を指定するがフィルタに入力された動画に依存する
* つまりオフセットを足し合わせて移行時間を引く必要がある
* afadeは移行時間を指定するだけでよしなにしてくれる
Tips:
* 画像は直に長さを指定すればよしなにしてくれる
* 画像の部分を無音にするにはadelayで時間を後に伸ばす
#>
$movies = @(
@{FileName="in.mkv";FadeInTime=0;From=(89.533);Time=(21)},
@{FileName="in.mkv";FadeInTime=1;From=(120+49.9);Time=(19.132)},
@{FileName="in.mkv";FadeInTime=1;From=(89.533);Time=(21)},
@{FileName="in.mkv";FadeInTime=1;From=(120+49.9);Time=(19.132)}
)
$filter_complex_v = ""
$filter_complex_a = ""
$offset_v_sum = 0
# Create filter_complex
for ($i = 1; $i -lt ($movies.Count); $i++) {
if ($i -eq 1) {
$offset_v_sum = $movies[$i-1].Time - $movies[$i].FadeInTime
$now_filter_v = ('[{0}:v][{1}:v]xfade=transition=fade:offset={2}:duration={3}[fv{1}]' -f ($i-1), $i, $offset_v_sum, ($movies[$i].FadeInTime))
$filter_complex_v = $filter_complex_v + $now_filter_v
$now_filter_a = ('[{0}:a][{1}:a]acrossfade=d={2}[fa{1}]' -f ($i-1), ($i), ($movies[$i].FadeInTime))
$filter_complex_a = $filter_complex_a + $now_filter_a
} else {
$offset_v_sum = $offset_v_sum + $movies[$i-1].Time - ($movies[$i].FadeInTime)
$now_filter_v = ('[fv{0}][{1}:v]xfade=transition=fade:offset={2}:duration={3}[fv{1}]' -f ($i-1), $i, $offset_v_sum, ($movies[$i].FadeInTime))
$filter_complex_v = $filter_complex_v + ";" + $now_filter_v
$now_filter_a = ('[fa{0}][{1}:a]acrossfade=d={2}[fa{1}]' -f ($i-1), $i, ($movies[$i].FadeInTime))
$filter_complex_a = $filter_complex_a + ";" + $now_filter_a
}
}
Write-Host "f-v" $filter_complex_v
Write-Host "f-a" $filter_complex_a
$filter_complex = $filter_complex_v + ";" + $filter_complex_a
Write-Debug $filter_complex
$AllArgs = @()
for ($i = 0; $i -lt $movies.Count; $i++) {
<# Action that will repeat until the condition is met #>
$AllArgs += '-ss'
$AllArgs += $movies[$i].From
$AllArgs += '-t'
$AllArgs += $movies[$i].Time
$AllArgs += '-i'
$AllArgs += $movies[$i].FileName
}
$AllArgs += '-filter_complex'
$AllArgs += ('{0}' -f $filter_complex)
$AllArgs += '-map'
$AllArgs += ('[fv{0}]' -f ($movies.Count - 1))
<#
$AllArgs += '-map'
$AllArgs += ('[outv]')
#>
$AllArgs += '-map'
$AllArgs += ('[fa{0}]' -f ($movies.Count - 1))
$AllArgs += '-r'
$AllArgs += '30'
$AllArgs += '-c:v'
$AllArgs += 'libsvtav1'
$AllArgs += '-preset:v'
$AllArgs += '7'
$AllArgs += '-tune:v'
$AllArgs += 'hq'
$AllArgs += '-crf:v'
$AllArgs += '45'
#$AllArgs += '-b:v#'
#$AllArgs += '5M'
$AllArgs += '-pix_fmt'
$AllArgs += 'yuv420p'
$AllArgs += '-shortest'
$AllArgs += 'concat.mkv'
# $ArgStr = $AllArgs -join ' '
& 'ffmpeg' $AllArgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment