Handbrake Discord & Telegram Notifications

This post is over a year old, the information may no longer be up to date.

I bought a new computer at the end of 2021 as my daily driver until then was a MacBook Air from 2018. Although it was good enough for what I was doing at the time, it was always limited heavily on some of the things I wanted to do, one of those things was transcoding video files to try and save some space on my NAS. As I started transcoding more files I was leaving my computer to do its thing for extended periods of time and checking occasionally to see if it had finished, although Handbrake does have some default “when done” options; they are pretty limited.

I started having a google online and came across this GitHub Issue on the official Handbrake repo from 2018 that is still open. There’s two options in Handbrake for when encoding is finished, Queue Completed and Encode Completed. Queue Completed doesn’t have an option for sending a command to an external program such as PowerShell, whereas Encode Completed does. So the limitation we have is that we get a notification every time an encode is completed but I’d rather that then nothing, at least for now until it annoys me. There’s a complete of pre-requirements before being able to do this but they are limited.

  • A copy of Handbrake
  • PowerShell 5.1 (minimum)
  • A Telegram Account or Discord Server

# Preparing

The script I’ve created has two options to choose from. Depending on which one you want to go with, either Discord or Telegram depends on which section you should follow below. You don’t need to do both unless you’re planning to use both eventually or modify the script to use both at the same time.

If you already know how to create a Telegram Bot or Discord Webhook and gather the required information, you can skip to Set Up.

# Discord

As mentioned in the pre-requirements you need to have a Discord Server to create a Webhook. Purely because there is already tutorials for creating a server I’m not going to explain it, however Discord do have a support article on how to do it.

Once you have your own Discord Server created click on your server name on the top left, go to Server Settings and click on Integrations.

Screenshot of Discord Integrations page

You’ll then want to click on Webhooks and create a New Webhook. The information here will only be seen by you but you can give it a fancy name and profile picture that will show up in the chat, I usually name mine after the tool in use, such as Radarr, Handbrake, Sonarr, etc. Once created you’ll want to copy the webhook URL and save it somewhere for later, we’ll need this for the PowerShell script.

The Webhook URL should look something like https://discord.com/api/webhooks/123456789/Abcdefg12-Abcdefghijklmnopqrstuvwxyz1928.

# Telegram

As mentioned in the pre-requirements you need to have a Telegram account already. You can get available downloads for Telegram from their website. Once you’ve created an account if you don’t already have one come back here and continue.

Open Telegram on your chosen device and message ‘BotFather’, or use this link t.me/botfather. Start a new chat and use the /newbot command and fill out the information as required.

Once the bot is created keep a copy of the token as this will be needed for the PowerShell script. It should look something like 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw. Along with the Token you also need a copy of the ChatID between you and the bot. There are multiple different ways of doing this but I did it by going to q3qkk.csb.app/ and pasting my bot token in and then sending it a message, after clicking Get Chat ID the Chat ID should appear and be a collection of numbers, again, keep this safe for later.

# Set up

Now that you have the information required, open Handbrake on your chosen machine and go to Preferences > When Done.

Screenshot of Handbrake Preferences

Ensure that the checkbox under Encode Completed is checked like above and then click Browse and select Powershell.exe in this location: C:\Windows\System32\WindowsPowerShell\v1.0\.

You’ll then want to copy the below script and save it on your system somewhere as a .ps1 file, I run mine from a directory on my C drive called ‘Development’ as that’s where all my scripts sit, but it’s entirely up to you where it runs from.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<#

.SYNOPSIS
Sends a message to an external provider such as Discord or Telegram

.DESCRIPTION
Script that allows sending a pre-defined message to Discord or Telegram.  Useful for Handbrake when conversions are completed.

.PARAMETER messageSystem
Which message system you want to use, Discord or Telegram.

.NOTES
    File Name  : handbrake_msg.ps1
    Author     : Laim McKenzie (https://laim.scot)
    Copyright 2022 - Laim McKenzie

.LINK
    https://laim.scot

.EXAMPLE
    .\handbrake_msg.ps1 -messageSystem Discord

.EXAMPLE
    .\handbrake_msg.ps1 -messageSystem Telegram

#>

param (

    [Parameter(Mandatory=$true)]
    [string]$messageSystem

)

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Discord Webhook Function
function Send-Discord {

    [string]$discordHook = 'DISCORD_WEBHOOK_HERE';

    [string]$content = "Handbrake conversion has now finished. @everyone"

    ## Don't change below this line ! :) 

    $payload = [PSCustomObject]@{

        content = $content

    }

    [bool]$hasError = $false

    if([string]::IsNullOrEmpty($discordHook)) {
        $hasError = $true
    }

    if([string]::IsNullOrEmpty($content)) {
        $hasError = $true
    }

    if(!$hasError) {
        Invoke-RestMethod -Uri $discordHook -Method Post -Body ($payload | ConvertTo-Json) -ContentType "application/json"
    } else {
        Write-Error "One or more required values are missing.  Have you populated the hookUrl and content in Send-Discord?"
    }

}

# Telegram Bot Function
function Send-Telegram {


    $telegramToken = "TOKEN_HERE"
    $telegramChatID = "CHAT_ID_HERE"

    $content = "Handbrake conversion has now finished."

    # Don't change below this line unless you know what you're doing.  
    # You can read up on the Telegram Documentation if you want to include some more stuff. It goes into the $payload
    # Documentation: https://core.telegram.org/bots/api#sendmessage
    
    $payload  = @{

        "chat_id" = $telegramChatID
        "text" = $content

    }

    Invoke-RestMethod -Uri ("https://api.telegram.org/bot{0}/sendMessage" -f $telegramToken) -Method Post -ContentType "application/json" -Body ($payload | ConvertTo-Json)

}

# Runs the function depending on the parameter passed
switch ($messageSystem) 
{

    "Discord" { Send-Discord }
    "Telegram" { Send-Telegram }
    default { "Unknown parameter value.  Options are Discord or Telegram." }

}

Depending on which service you’re going to use, you’ll need to change a variable or two. For Telegram you’ll want to change $telegramToken and $telegramChatID to their required values as gathered earlier, for Discord you’ll want to change $discordHook to your Webhook URL.

Now that you have the script saved somewhere and the required information in it, go back to Handbrake and in the Arguments field type in the following. If you’re using Discord replace the parameter -messageSystem to Discord, if you’re using Telegram replace the parameter -messageSystem to Telegram.

1
-Command C:\PATH_TO_FILE\FileName.ps1 -messageSystem ServiceName

For example, mine is

1
-Command C:\Development\handbrake_msg.ps1 -messageSystem Discord

# Limitations

There is one major limitation to using PowerShell for the notification sender. Unfortunately when a script is ran against powershell.exe it does for a brief second open a PowerShell window, so if you’re in the middle of doing something else like playing a game it can be a bit annoying.

If you have a dedicated machine for transcoding however, this won’t be an issue. I just thought it would be worth mentioning as it has caught me out a few times even knowing it can be an issue.

//

Song Addiction at he moment: ilyTOMMY - SICK & TIRED

Catch. 😊x