The Slayer The Slayer - 3 months ago 55
PowerShell Question

Powershell and FFMPEG - FFMPEG not executing

So I wrote a powershell script that recursively searches all video files above a certain size and then resizes them.

I feel like I'm missing something obvious here...because my script doesn't actually run FFMPEG, it just displays the command to run it on the screen. I'm sure I'll facepalm at the solution.

$SearchPath = "N:\baseball"

$oldVideos = Get-ChildItem -Include @("*.mkv", "*.mov", "*.mpg", "*.wmv", "*.avi") -Path $SearchPath -Recurse | where-object {$_.length -gt 500MB};

Set-Location -Path 'C:\Program Files\ffmpeg\bin';

foreach ($OldVideo in $oldVideos)
{
$outputfolder = "O:\resized"
$oldname = Get-Item $oldvideo | Select-Object -ExpandProperty BaseName
$suffix = "_resized.mp4"
$newname = "$($oldname)_$($suffix)"
$ffmpeg = ".'C:\Program Files\ffmpeg\bin\ffmpeg.exe'"
$arguments = " -i `"$($OldVideo)`" -vf scale=720:trunc(ow/a/2)*2 -c:v libx264 -f mp4 `"$outputfolder\$newname`" -y"
$ffmpeg + $arguments}


Here's the actual output to the screen when I run the script
.'C:\Program Files\ffmpeg\bin\ffmpeg.exe' -i "N:\baseball\hitting\067.MOV" -vf scale=720:trunc(ow/a/2)*2 -c:v libx264 -f mp4 "O:\resized\067__resized.mp4" -y

That command should execute (it runs in a command window).

Answer

I finally got this working...I looked at a couple other scripts and how they were executing...seems like I made this overly complicated. EDIT: I added logging and additional functionality to delete the original and move it to the original location. I probably should add some error-handling to check to see if the movie length is identical...gotta figure that out first. NOTE: Logging doesn't quite work right...but the core part of the script (finding movies above 500MB, resizing, moving back to the original folder and deleting works). I also need to add 2 checks to the movie files. 1 to see if the resized movie is actually smaller and if the movie durations are identical.

 $env:Path += ";C:\Program Files\ffmpeg\bin\"

function Write-Log 
    { 
        param
        (
            [string]$strMessage
        )

            $LogDir = 'L:\conlogs\'
            $Logfile = "\Conversion-Log.txt"
            $Path = $logdir + $logfile
            [string]$strDate = get-date
            add-content -path $Path -value ($strDate + "`t:`t"+ $strMessage)
}

$SearchPath = "N:\baseball"

$oldVideos = Get-ChildItem -Include @("*.mkv", "*.mov", "*.mpg", "*.wmv", "*.avi","*.mp4") -Path $SearchPath -Recurse | where-object {$_.length -gt 500MB} | where-object {$_.Name -NotMatch "resized"};

foreach ($OldVideo in $oldVideos) 
{   $outputfolder = "O:\resized"
    $oldname = Get-Item $oldvideo | Select-Object -ExpandProperty BaseName
    $suffix = "resized.mp4"
    $newname = "$($oldname)_$($suffix)"
    ffmpeg.exe -i $oldvideo.FullName -vf "scale=720:trunc(ow/a/2)*2" -c:v libx264 -f mp4 -y "$outputfolder\$newname"

    $OriginalSize = (Get-Item $OldVideo).length 
    $ConvertedSize = (Get-Item $outputfolder\$Newname).length 

    If($ConvertedSize -le $OriginalSize)
    {
        Write-Log "$($NewVideo) has been successfully resized"
        Remove-Item $OldVideo

        If (Test-Path $OldVideo)
        {
            write-log "Unable to remove $($OldVideo)"
        }

        Else
        {
            write-log "Successfully removed $($OldVideo)"
        }
        Elseif
        {
            write-log "You dun goofed!"
        }

        $oldvidpath = [System.IO.Path]::GetDirectoryName($oldvideo)
        Move-Item "$outputfolder\$Newname" -Destination $oldvidpath

       If (Test-Path "$oldvidpath\$newname")
        {
            write-log "Unable to move $($newname)"
        }

        Else
        {
            write-log "Successfully moved $($OldVideo)"
        }

        Elseif
        {
            write-log "You dun goofed!"
        }
    }
}
Comments