Tuesday, March 19, 2024

How to generate a notifications once Handbreak finished its current work?

When I create trainings I normally record them and hand them out to the users who attend the training, so they can watch them again. The issue here is that the software I´m using here couldn´t make use from H.265 (x265) so I need to use Handbreak here to convert that. Unfortunately handbreak do not come up with any notification features to inform me when it finished his work (and depending on the training it sometimes has 3-5 hours of video material to work with). So its I time to build something to fill the gap.

Currently the Open Source Video Transcoder handbreak 1.0.7 (https://handbrake.fr) do not offer a notification features. So if a queue is finished there is no way to inform you. However in the preferences it has a feature called “Send File to” which can be used to do anything we need here and would require only some lines of code. However storing the password as plain text inside the powershell script, so that it would be very easy for everyone (3rd party persons, trojan, virus, …) to capture that is a very bad approach, we need to involve the Convert​From-​Secure​String here, which according to Microsoft ConvertFrom-SecureString make use from from DPAPI and encrypts the password so that it can be only read on that machine (keep noted that a 3rd party persons, trojan, virus, … can decrypt the password on the machine as well, so its only a security layer and no 100% secure solution). It might therefore also a good idea to build a 2nd mailbox here which will only be used to send out such notifications.

So if you wish to send an email once handbreak is finished and / or play an sound do the following:

1.) Make sure that you at least run powershell 3.0 (would prefer if you update to powershell 5.0 / Management Framework 5.0 via the download here). You can check the version you are currently running via:

$PSVersionTable.PSVersion

2.) Start a powershell (run as admin) and enter the following:

Set-ExecutionPolicy Unrestricted

Otherwise we need to sign our script and without that option it will not work then.

3.) Then inside powershell navigate to your script folder (if you do not have a Scripts create one)

cd “C:\Scripts”

4.) Then enter

Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File -FilePath HandBrake-Notify_securestring

and after that enter the password from the email account you wish to use here. The password is then saved in “C:\Scripts\HandBreak-Notify_securestring”.

If you changed your password simply delete the handbreak-Notify_securestring file in the folder and re-run the action.

5.) Inside your scripts folder (C:\Scripts) create also a file called HandBreak-Notify_Plugin.ps1

notepad.exe HandBreak-Notify_Plugin.ps1

6.) Inside that file copy and past the following (and adjust the mail server details) and save it

# Handbreak Notify Script V0.1
# By B. Wieczorek
# http://www.admin-enclave.com

# Note: You need powershell 3.0 (or higher here, would prefer Powershell 5.0)
#		To upgrade to PowerShell 5.0 simply install "Management Framework 5.0"
#		via(https://www.microsoft.com/en-us/download/details.aspx?id=50395)

# Configuration:
# =================

# General
$bPlaySound=1
$bSendEmail=1
$bDebug=0

# eMail Configuration
$From="Joe.Blocks@gmail.com"
$To="Martina.Mustermann@gmail.com"
$Subject="Handbreak finished finished the video transcoding!"
$Body="Dear user, handbreak finished finished the video transcoding!"
$SMTPServer="smtp.gmail.com"
$SMTPPort="587"
$SMTPUsername="Joe.Blocks@gmail.com"


# ------------------------------------------------------------------------------
if ($bSendEmail -eq 1)
{
    Write-Host " Will send an email now...."

    if ($bDebug -eq 1){ Write-Host " DEBUG: Will send an email" -foregroundcolor yellow}

    $SecureStringPassword=Get-Content -Path HandBrake-Notify_securestring | ConvertTo-SecureString
    $EmailCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $SMTPUsername,$SecureStringPassword
		
    Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential $EmailCredential
}

if ($bPlaySound -eq 1)
{

    Write-Host " Will play the Imperial March (Star Wars) now...."

    if ($bDebug -eq 1){ Write-Host " DEBUG: play sound" -foregroundcolor yellow}

    # The Imperial March (Star Wars)
    [console]::beep(440,500)
    [console]::beep(440,500)
    [console]::beep(440,500)
    [console]::beep(349,350)
    [console]::beep(523,150)
    [console]::beep(440,500)
    [console]::beep(349,350)
    [console]::beep(523,150)
    [console]::beep(440,1000)
    [console]::beep(659,500)
    [console]::beep(659,500)
    [console]::beep(659,500)
    [console]::beep(698,350)
    [console]::beep(523,150)
    [console]::beep(415,500)
    [console]::beep(349,350)
    [console]::beep(523,150)
    [console]::beep(440,1000)

}

7.) Now we need to test out script. Sometimes a antivirus client might block outgoing email connections. Then the script would fail and we need to configure it correctly. To test the script use the still open powershell prompt, and navigate to the scripts folder (e.g. C:\Scripts) then run:

.\HandBreak-Notify_Plugin.ps1

8.) Now open handbreak, click on tools -> Preferences -> General

9.) mark the “send file to” option, then browse to “C:\Windows\System32\WindowsPowerShell\v1.0\” and select the powershell.exe

10.) In the arguments dialog inside HandBreak enter:

-Command C:\Scripts\Handbreak-Notify_Plugin.ps1

11.) Test your solution with a small movie. Once it finished you should see that a powershell windows popup and then quickly disappear (if everything is working as expected). During that an email is generated and some sound appears.

If that script helps you feel free to leave a “thank you” comment.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

35FollowersFollow
- Advertisement -

Latest Articles