このようなファイルをパラメータとして受け取り、ffmpegコマンドを使って動画ファイルを切り抜くPowerShellスクリプトを作成してください。 このPowerShellスクリプトは次のような構文で呼び出し使用するものとします。 script.ps1 -labelfile 切り抜きポイントを指定したタブ区切りテキストファイル -source 切り抜き対象のmp4動画ファイル このスクリプトの実行後は、スクリプトファイルと同じフォルダにPick 1.mp4、Pick 2.mp4、Pick 3.mp4、Pick 4.mp4、Pick 5.mp4という五つのファイルが生成されることを期待します。
以下は、指定された条件を満たすPowerShellスクリプトの例です。このスクリプトは、指定されたタブ区切りテキストファイルとソース動画ファイルを受け取り、ffmpegを使用して指定範囲を切り抜き、指定した名前で動画ファイルを出力します。
script.ps1
param (
[string]$labelfile, # タブ区切りテキストファイルのパス
[string]$source # ソース動画ファイルのパス
)
# ffmpegコマンドの存在を確認
if (-not (Get-Command "ffmpeg" -ErrorAction SilentlyContinue)) {
Write-Error "ffmpegがインストールされていません。インストールしてください。"
exit 1
}
# 入力ファイルの確認
if (-not (Test-Path $labelfile)) {
Write-Error "指定されたラベルファイルが見つかりません: $labelfile"
exit 1
}
if (-not (Test-Path $source)) {
Write-Error "指定されたソース動画ファイルが見つかりません: $source"
exit 1
}
# ラベルファイルの内容を読み込む
$labels = Get-Content $labelfile | ForEach-Object {
$line = $_ -split "`t" # タブで分割
@{
Start = $line[0]
End = $line[1]
Output = "$PSScriptRoot\\\\$($line[2]).mp4"
}
}
# 各範囲を切り抜く
foreach ($label in $labels) {
$duration = [math]::Round([double]$label.End - [double]$label.Start, 3)
$outputPath = $label.Output
# ffmpegコマンドの実行
$command = @(
"ffmpeg",
"-i", "`"$source`"",
"-ss", $label.Start,
"-t", $duration,
"-c", "copy",
"`"$outputPath`""
) -join " "
Write-Output "実行中: $command"
Invoke-Expression $command
if (-not (Test-Path $outputPath)) {
Write-Error "出力ファイルの作成に失敗しました: $outputPath"
} else {
Write-Output "出力完了: $outputPath"
}
}
このスクリプトは、次のように実行します:
.\\\\script.ps1 -labelfile "C:\\\\path\\\\to\\\\SBC.オープンマイク 103 チェックポイント.txt" -source "C:\\\\path\\\\to\\\\source_video.mp4"
ffmpeg
がシステムにインストールされている必要があります。生成される動画ファイルは、Pick 1.mp4
, Pick 2.mp4
のように命名されます。
以下のようにスクリプトを修正し、ffmpeg
コマンドに -y
オプションを追加することで、既存のファイルがあれば常に上書きするように設定できます。
script.ps1
param (
[string]$labelfile, # タブ区切りテキストファイルのパス
[string]$source # ソース動画ファイルのパス
)
# ffmpegコマンドの存在を確認
if (-not (Get-Command "ffmpeg" -ErrorAction SilentlyContinue)) {
Write-Error "ffmpegがインストールされていません。インストールしてください。"
exit 1
}
# 入力ファイルの確認
if (-not (Test-Path $labelfile)) {
Write-Error "指定されたラベルファイルが見つかりません: $labelfile"
exit 1
}
if (-not (Test-Path $source)) {
Write-Error "指定されたソース動画ファイルが見つかりません: $source"
exit 1
}
# ラベルファイルの内容を読み込む
$labels = Get-Content $labelfile | ForEach-Object {
$line = $_ -split "`t" # タブで分割
@{
Start = $line[0]
End = $line[1]
Output = "$PSScriptRoot\\\\$($line[2]).mp4"
}
}
# 各範囲を切り抜く
foreach ($label in $labels) {
$duration = [math]::Round([double]$label.End - [double]$label.Start, 3)
$outputPath = $label.Output
# ffmpegコマンドの実行
$command = @(
"ffmpeg",
"-y", # 上書きオプションを追加
"-i", "`"$source`"",
"-ss", $label.Start,
"-t", $duration,
"-c", "copy",
"`"$outputPath`""
) -join " "
Write-Output "実行中: $command"
Invoke-Expression $command
if (-not (Test-Path $outputPath)) {
Write-Error "出力ファイルの作成に失敗しました: $outputPath"
} else {
Write-Output "出力完了: $outputPath"
}
}
y
オプションを ffmpeg
コマンドに追加しました。これにより、同じ名前のファイルが既に存在していても確認を求めずに上書きします。