我錄制了一些相同長度為 40 秒的視頻。我有同名的舊視頻,但長度在 28-38 秒之間變化。我希望將新視頻剪輯成與舊視頻相同的長度。新舊同名。是否可以使用 ffmpeg 撰寫腳本?
獲取舊視頻的長度,然后將具有相同名稱的新視頻剪切為與舊視頻相同的長度。Windows powershell 或命令 bat 檔案。
只需要剪掉視頻的結尾
uj5u.com熱心網友回復:
如果您在 PATH 環境變數中ffprobe.exe有路徑
(如),則可以使用以下代碼:ffmpeg.exe'C:\Program Files\ffmpeg\bin\'
# the folders where the old and the new videos can be found
$oldVideoPath = 'X:\path\to\OLD\videos'
$newVideoPath = 'X:\path\to\NEW\videos'
# get a list of the old video files; filenames only
$oldVidNames = (Get-ChildItem -Path $oldVideoPath -File).Name
# get the new video files and loop over them
(Get-ChildItem -Path $newVideoPath -File) |
Where-Object { $oldVidNames -contains $_.Name } | ForEach-Object {
# construct the full path to the old video from which we take the duration
$oldVideo = Join-Path -Path $oldVideoPath -ChildPath $_.Name
# construct the full path to the trimmed output file (append '_trimmed')
$trimmedVideo = Join-Path -Path $newVideoPath -ChildPath ('{0}_trimmed{1}' -f $_.BaseName, $_.Extension)
# get the duration of the old video in format HH:mm:ss.ffffff
$oldDuration = ffprobe -i "$oldVideo" -show_entries format=duration -v quiet -of csv="p=0" -sexagesimal
Write-Host "Trimming '$($_.FullName)' to duration $oldDuration"
# now trim the new video to the same length, overwriting the original by using switch '-y'
ffmpeg -y -ss 00:00:00 -i "$($_.FullName)" -to $oldDuration -c:v copy -c:a copy "$trimmedVideo" -loglevel quiet
}
如果您沒有該 PATH 環境變數,則需要使用包含 ffprobe 和 ffmpeg 的完整路徑和檔案名的變數,如下所示:
# the file paths to both ffprobe and ffmpeg if you do not have the path to the bin folder in your PATH environment variable
$ffprobe = 'C:\Program Files\ffmpeg\bin\ffprobe.exe'
$ffmpeg = 'C:\Program Files\ffmpeg\bin\ffmpeg.exe'
# the folders where the old and the new videos can be found
$oldVideoPath = 'X:\path\to\OLD\videos'
$newVideoPath = 'X:\path\to\NEW\videos'
# get a list of the old video files; filenames only
$oldVidNames = (Get-ChildItem -Path $oldVideoPath -File).Name
# get the new video files and loop over them
(Get-ChildItem -Path $newVideoPath -File) |
Where-Object { $oldVidNames -contains $_.Name } | ForEach-Object {
# construct the full path to the old video from which we take the duration
$oldVideo = Join-Path -Path $oldVideoPath -ChildPath $_.Name
# construct the full path to the trimmed output file (append '_trimmed')
$trimmedVideo = Join-Path -Path $newVideoPath -ChildPath ('{0}_trimmed{1}' -f $_.BaseName, $_.Extension)
# get the duration of the old video in format HH:mm:ss.ffffff
$oldDuration = & $ffprobe -i "$oldVideo" -show_entries format=duration -v quiet -of csv="p=0" -sexagesimal
Write-Host "Trimming '$($_.FullName)' to duration $oldDuration"
# now trim the new video to the same length, overwriting the original by using switch '-y'
& $ffmpeg -y -ss 00:00:00 -i "$($_.FullName)" -to $oldDuration -c:v copy -c:a copy "$trimmedVideo" -loglevel quiet
}
ffmpeg 開關:
-y overwrite output file if exists
-i specifies the input file
-ss the startposition in the input file (here `00:00:00` to start from the very beginning)
-to specifies duration from start to end (this value is taken from the old file with the same name)
-c:v copy trim video via stream copy (fast)
-c:a copy trim audio via stream copy (fast)
-loglevel quiet do not send output via error stream to console
ffprobe 開關:
-i specifies the input file
-show_entries format=duration set list of entries to show, in this case we only want duration
-v quiet do not send output via error stream to console
-of csv="p=0" set output printing format to csv; do not print section name
-sexagesimal Use sexagesimal format HH:MM:SS.MICROSECONDS for time values.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/435596.html
標籤:电源外壳
上一篇:我搜索如何匹配我的最后一個字符
