Last active
August 13, 2024 07:40
-
-
Save nanoDBA/a6a946f02fe1118e4533fa257baf0ff4 to your computer and use it in GitHub Desktop.
FFmpeg Remove video frames without motion and remove audio track
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$filename = "A:\Users\kilroy\Remove_lack_of_motion.mp4" | |
# Use ffprobe to extract the avg_frame_rate | |
$fpsFraction = ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 "$filename" | |
$fps = Invoke-Expression "($fpsFraction)" | |
# Convert fractional fps to a numeric value if necessary | |
if ($fpsFraction -match "/") { | |
$fpsParts = $fpsFraction -split "/" | |
$fps = [double]$fpsParts[0] / [double]$fpsParts[1] | |
} | |
# Use ffprobe to print detailed information about the input file | |
ffprobe -v error -show_entries format=duration:stream=index,codec_name,codec_type,width,height,bit_rate -of default=noprint_wrappers=1 "$filename" | |
$fps = [math]::truncate($fps * 100) / 100 #truncate to 2 decimal places | |
# Display the fps | |
Write-Output "Frames per second: $fps" | |
# create new file without audio and removing frames lacking motion | |
$sourceFrameRate = $fps | |
$newFilename = "$(dir $filename | foreach { Join-Path -Path $_.Directory -ChildPath $_.BaseName })_remove_nonmotion_mute_audio.mp4" | |
ffmpeg.exe -i "$($filename)" -vf "select=gt(scene\,0.0002),setpts=N/($($sourceFrameRate)*TB)" -an "$($newFilename)" | |
<# -an remove audio #> | |
<# -vf "select=gt(scene\,0.001),setpts=N/(<framerategoeshere>*TB)" #> | |
<# https://superuser.com/questions/984841/ffmpeg-remove-parts-without-motion/1029175#1029175 | |
"The higher the number, the more change between frames is ignored, | |
in quick testing you might need to go as low as 0.00001-0.00005 | |
depending on the kind of footage you're dealing with." #> | |
# https://video.stackexchange.com/questions/20229/ffmpeg-trim-filter-renders-the-same-length-output-as-input-with-trimmed-out-fram/20230#20230 | |
# https://www.videoproc.com/resource/ffmpeg-commands.htm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment