1

Here is my little script I am trying to get working. It uses handbrake from the cli to convert an avi. After that I am trying to zip the file with 7zip cli, I cant use the 7zip powershell package. If I run the whole script it will get to the part of zipping and just error out. I have tried running it as a cmd and start process. Its like its not finishing the foreach and just running the next command. The command works if I run it by itself and an already converted file. What am i missing here?

$date = Get-Date -Format yyyy-MM-dd
    $filelist = Get-ChildItem C:\dan\VideoConvert\*.avi
     $i = 0

    ForEach ($file in $filelist)
    {
        $i++
        $oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;
        $newfile = $file.DirectoryName + "\converted$i.mp4";
        Start-Process "C:\dan\VideoConvert\HandBrakeCLI.exe" -ArgumentList "-i `"$oldfile`" -o `"$newfile`"" -Wait
    }
    & "c:\dan\VideoConvert\7z.exe" a -t7z "$date" *.mp4
    #Start-Process "c:\dan\VideoConvert\7z.exe" -ArgumentList "a -t7z $date.7z *.mp4 -p"
    #Copy-Item c:\dan\VideoConvert\$date.7z \\ssb.local\shares\temp
    #Remove-Item c:\dan\VideoConvert\$date.7z
mild0d
  • 212
  • 1
  • 3
  • 8

2 Answers2

1

In the Start-process command, try including the -wait option, to wait for the zip action to complete before process its output.

Tip: use the 7z.exe Switch -mx0 (Don't compress at all), this will increase your speed, because it won't try to further compress the already compressed mp4 format.

BigJoe
  • 131
  • 2
0

I was able to work this out with the help of Roberts comment and using the -mx=0 flag. Thanks! Here is a working copy.

$date = Get-Date -Format yyyy-MM-dd
$filelist = Get-ChildItem C:\dan\VideoConvert\*.avi
$pass = Read-Host -Prompt 'Enter Password for archive:  '
$i = 0
function CopyDelete ()
{
    Copy-Item c:\dan\VideoConvert\"$date.7z" \\ssb.local\shares\temp
    Remove-Item c:\dan\VideoConvert\*.7z
    Remove-Item c:\dan\VideoConvert\*.mp4
}
function Process7z ()
{
    Start-Process "c:\dan\VideoConvert\7z.exe" -ArgumentList "a -t7z -mx=0 C:\dan\VideoConvert\$date.7z c:\dan\VideoConvert\*.mp4 -p$pass" -wait
}
function ProcessVideo ()
{
    ForEach ($file in $filelist)
    {
        $i++
        $oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;
        $newfile = $file.DirectoryName + "\converted$i.mp4";
        Start-Process "C:\dan\VideoConvert\HandBrakeCLI.exe" -ArgumentList "-i `"$oldfile`" -o `"$newfile`"" -Wait -NoNewWindow
    }   
}
ProcessVideo
Process7z
CopyDelete
mild0d
  • 212
  • 1
  • 3
  • 8