0

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).

4
  • you just build some string variables. nowhere do you actually USE those variables. Commented Aug 4, 2016 at 21:13
  • At the end I'm using $ffmpeg + $arguments Commented Aug 4, 2016 at 21:17
  • you might be able to use invoke-expression for this Commented Aug 4, 2016 at 21:19
  • When I run the command manually in powershell, it errors out. ow/a/2 : The term 'ow/a/2' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:121 Commented Aug 4, 2016 at 21:27

2 Answers 2

2

Right now you're just combining two strings, so it outputs the combined string. It's doing exactly what you ask it to. Try changing it to this:

$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}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, on the right track. Now the issue is that the $OldVideo output is no longer quoted so the script aborts if the filename or directory name has spaces.
it should still be quoted, that part should not have changed at all.
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.