'FFMPEG code is not trimming the videos properly
I am trying to trim videos from a specific folder using ffmpeg via powershell but it seems the out doesn't work. However, the files are being renamed as per the code.
<#
This script trims videos with ffmpeg using a start and end time.
Code below that calls ffmpeg and ffprobe assumes those executables
are included in the return for $env:path. If this is not the
case, either add them to PATH or call the .exe using the full
path.
#>
$files = Get-ChildItem -Filter "*.mp4" -LiteralPath "C:\Users\PC\Desktop\Sample" # create a list of files to process
$bool_fast = $true # use a fast method to trim the video file. Set to $false to use the slow method.
ForEach ($f in $files) {
# set up file names
$src = $dest = $f.FullName # store original file name
$new_name = "$($f.BaseName)_LONGER$($f.Extension)" # create a new file name for original file by inserting "_LONGER" before the extension
Rename-Item -Path $src -NewName $new_name # rename the original file so we can use its name for the output (ffmpeg can't overwrite files)
$src = $dest.Replace($f.Name, $new_name) # store the new source file name
# get the file length in seconds
$strcmd = "ffprobe -v quiet -print_format compact=print_section=0:nokey=1:escape=csv -show_entries format=duration $src" # outputs seconds
$len = Invoke-Expression $strcmd
# set start and end times
$start = 0 # where to start trim in seconds from the beginnning of original video
$end = 4 # where to end trim in seconds from the beginnning of original video
# trim the video
If ($bool_fast) {
# this method seeks to the nearest i-frame and copies ... fast
$strcmd = "ffmpeg -i $src -ss $start -to $end -c:v copy -c:a copy $dest"
} Else {
# this method re-encodes the file ... slow, but more accurate adherence to a time specification
$strcmd = "ffmpeg -i $src -ss $start -to $end $dest"
}
Invoke-Expression $strcmd
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
