Jump to content
PenkethBoy

Post Processing Script - Working Example - Powershell

Recommended Posts

PenkethBoy

Having read this thread over the last couple of days https://emby.media/community/index.php?/topic/58828-convert-recordings-to-mkv-option-removed/ and wanting to try out the Post Processing option in the Emby DVR, for a while, i spent an hour or so experimenting to come up with a basic working method for people to use and amend for their needs.

 

The current Emby convert feature does not meet my needs and i have several PowerShell scripts i use in conjunction with ffmpeg to process video files.

 

I have use one of my scripts as the basis of the script below with some adjustments/simplifications to work with Emby's Post Processing option.

 

To make the script work we first have to setup the Post Processing options.

 

5b94bd7ad227e_Capture1a.jpg

 

Powershell can be called directly without needing to provide its full path.

 

The command line arguments are to tell Powershell (PS)

1. A command line of arguments is coming                            -command "& .... "

2. Where to find the PS Script                                                '<path to file>'

3. Where to find the video file to be processed                     -oldVideo '{path}' 

4. Additional parameters defined in the PS Script                 -InType ts -OutType mkv -DelFile

NOTE: the double and single quotes are important don't forget to use them correctly as shown

 

The parameters

-oldVideo, -InType, -OutType and -DelFile are defined in the PS script - see top of the script file

5a. -InType ts     -Intype is the parameter and 'ts' is one of the types of file the script can accept - for Emby Post Processing thats going to be ts

5b. -OutType mkv   -OutType is the parameter and 'mkv' is one of two file types the script can accept - as written

The only parameter that is optional is -DelFile - if provided it will delete the video file after processing by ffmpeg - if omitted the "old" video file will remain.

[CmdletBinding()]
param(
  # Parameters 
  [parameter(Mandatory=$True)]
  [string]$oldVideo,
  [parameter(Mandatory=$True)][ValidateSet("mp4","mkv","ts")]
  [string]$InType,
  [parameter(Mandatory=$True)][ValidateSet("mp4","mkv")]
  [string]$OutType,
  [parameter(Mandatory=$False)]
  [switch]$DelFile
)
<#=============================================================================
EmbyCMD v0.1.0 - 09/09/2018

Script that can be run from Emby Post Processing DVR
Created by PenkethBoy
=============================================================================#>

# Amend if in and out file type the same 
If ($InType -eq $OutType)
{
    $OutExt = "-1."+$OutType
}
else
{
    $OutExt = "."+$OutType
}

# New Video name and ext
$newVideo = Join-Path (Split-Path -path $oldVideo) ((Get-ChildItem -Path $oldVideo).BaseName + $OutExt)

# Declare the command line arguments for ffmpeg.exe - in this case a reencode of video and audio, ignore subtitles(remove) and remove chapters
# replace with your chosen ffmpeg switches between "{0}" and "{1}"
$ArgumentList = '-i "{0}" -profile:v high -level:v 4.1 -c:a aac -strict experimental -b:a 128k -ac 2 -map_chapters -1 "{1}"' -f $oldVideo, $newVideo;

# Kick off ffmpeg - with ffmpeg at least stderr and stdout appear reversed!
# Will generate a txt file named the same as newvideo with ffmpeg output within i.e. -RedirectStandardError "$newVideo.txt"
# This will run as a hidden window i.e. -WindowStyle Hidden

# Replace the ffmpeg path with your path to ffmpeg or other application - make sure its in the ""
Start-Process -FilePath "F:\Video\ffmpeg\bin\ffmpeg.exe" -ArgumentList $ArgumentList -Wait -RedirectStandardError "$newVideo.txt" -WindowStyle Hidden

If ($DelFile)
{
    Remove-Item -Path $oldVideo -Force
}

Exit  

Save the script above as a text file with a .ps1 extension and call it what you like.

The only amendment necessary to the script for it to run is to amend the ffmpeg path to your environment.

The script comes with an ArgumentsList for ffmpeg - in this case a re-encode of the original file. If this is not what you want - change the arguments to your preference and follow the notes in the script !

The script will create a txt file with all the ffmpeg output in the same location as the original file - unsurprisingly it looks very similar to an Emby transcode log file.

The new video file is also located alongside the original file with the extension you provided for the -OutType parameter

The original will be deleted if you provided the -DelFile parameter. 

 

The script can also be used directly in a PS console window outside of Emby and is why you have the -InTYpe and -OutType parameters.

 

Notes on Powershell

Powershell 5.1 is available for Windows 7 SP1 and up so that should accommodate 99% of people. Search for "Windows Management Framework 5.1" and follow the instructions to install.

Powershell Core 6/6.1 is available for Linux and MacOS and the script should run fine as its not doing anything Windows specific.

 

For those of you new to Powershell you will need to set your Powershell Execution Policy for either the machine or the windows user who will "run" the scripts. The Execution Policy sounds all grand and pompous but remember this is Microsoft speak for protecting you from a dumbass mistake - i.e. running a script by mistake by double clicking it. What its not is some virus or security protection measure that's worth anything. Its up to you what you are happy with, so set the Execution Policy at the appropriate level for you, after you have read up on it. Me? I have it unrestricted but make your own choice. Search for "Powershell Execution Policy". Depending on what Execution Policy you choose may affect the running of the script - dont be too restrictive as it needs to run unattended.

 

Emby Logs

When the Post Processing is initiated by Emby - you will see in the log a line like this

 

Info App: Running recording post processor powershell -command "& 'd:\Temp\embyCMD.ps1' -oldVideo 'Z:\Recordings\Series\Thomas and Friends Dinos & Discoveries\Season 19\Thomas and Friends Dinos & Discoveries S19E02 Timothy & The Rainbow Truck.ts' -InType ts -OutType mkv -DelFile"

 

And another like this

 

Info App: Recording post-processing script completed with exit code 0

 

If the Exit code is 1 - PS had an issue and likely the script failed you need to investigate.

 

If you want to modify the script as it is very basic - go ahead and play you may even learn something  :D

 

Think that's about it

 

Good Luck

 

PenkethBoy

 

 

  • Like 6

Share this post


Link to post
Share on other sites
PenkethBoy

NOTE - 11/9/2018

 

To be clearer the script above does not do any error checking to see if the ffmpeg process failed to convert the ts file and if you have the -DelFile option enabled it will just delete the ts file.

 

Error checking could be added by the user although from reading a few threads on ffmpeg - getting a valid exit code of 0 (zero) does not always mean the conversion worked! - sigh

 

So use the -DelFile with caution as recorded LiveTV is prone to errors in the ts stream - some of which may cause ffmpeg to abort.

  • Like 1

Share this post


Link to post
Share on other sites
Kramerika

This works with a batch file but not with post-processing.  Even though it compresses/converts the file, it does not recognize the second command to delete the original file.

 

HandBrakeCLI -i "{path}" -o "{path}".mkv -f av_mkv -a 1 -E copy:ac3 -e x264 -q 22.0 --cfr -r 29.97 --loose-anamorphic --encoder-preset medium --encoder-profile high --encoder-level 4.1 && del "{path}"

 

How would I convert this to a powershell script using the post-processing?

Share this post


Link to post
Share on other sites
PenkethBoy

why are you using a bat file?

  • Like 1

Share this post


Link to post
Share on other sites
Kramerika

I was using a batch file to test the command line options without having to retype all of that over and over on the command prompt.  I should not have mentioned that as it was not important.  My point was that those commands worked outside of post-processing from a Windows command prompt but not with post-processing in Emby.

 

In any case, what I was hoping you could help me with is how to substitute HandbrakeCLI and commands into your script replacing ffmpeg and commands as I have not used powershell and am a bit confused as to what is related to ffmpeg and what is related to powershell in your script.  These are the command line arguments that I need to pass to HandbrakeCLI.exe:

 

-i recording.ts -o recording.mkv -f av_mkv -a 1 -E copy:ac3 -e x264 -q 22.0 --cfr -r 29.97 --loose-anamorphic --encoder-preset medium --encoder-profile high --encoder-level 4.1

Share this post


Link to post
Share on other sites
PenkethBoy

first lets get it to work as is

 

Can you do a screenshot showing what you have in the Emby Post Processing section - i.e the same as i did in post #1

Share this post


Link to post
Share on other sites
Kramerika

post-67967-0-61272100-1536796607_thumb.png

 

It works as is (the application + command line options) for post processing in Emby unless you mean with powershell.  The only part that didn't work for me was executing a second command to delete the original file.  That's why I wanted to try your script but use Handbrake instead.  The issue is that I don't know what to substitute where in your script in terms of the application and command line options.

 

This is what works for the application and arguments.  It does convert the file after the recording is finished.

 

D:\Recordings\HandbrakeCLI.exe

 

-i "{path}" -o "{path}".mkv -f av_mkv -a 1 -E copy:ac3 -e x264 -q 22.0 --cfr -r 29.97 --loose-anamorphic --encoder-preset medium --encoder-profile high --encoder-level 4.1

 

The addition that I made to the arguments in an attempt to delete the original file that did not work was

 

&& del {path}

 

Do I just replace the file path with the path to HandbrakeCLI.exe and replace the arguments with the ones above or do I need to remove/change other parts of the script to work with Handbrake versus ffmpeg?

Share this post


Link to post
Share on other sites
PenkethBoy

Ok - so after going back and forth - you are not saying my script does not work

 

What you are saying is you cant get your setup to work with the file being deleted after the conversion.

Share this post


Link to post
Share on other sites
PenkethBoy

the reason && del(path) does not work - i believe is handbrake does not support that command and its ignored/or raises an error thats not visible

 

within my script if you change the ArgumentsList it should look like this

 

$ArgumentList = '-i $oldvideo -o $NewVideo -f av_mkv -a 1 -E copy:ac3 -e x264 -q 22.0 --cfr -r 29.97 --loose-anamorphic --encoder-preset medium --encoder-profile high --encoder-level 4.1'

or 

$ArgumentList = '-i "{0}" -o "{1}" -f av_mkv -a 1 -E copy:ac3 -e x264 -q 22.0 --cfr -r 29.97 --loose-anamorphic --encoder-preset medium --encoder-profile high --encoder-level 4.1' -f $oldvideo, $newvideo;

 

And change the ffmpeg path to the handbrakecli.exe path - keep the double quotes

 

NOTE:

One thing i dont know for Handbrake is if the output of the process will be returned in -RedirectStandardError or not so you will need to try a test to see if the txt file is empty or not.

  • Like 1

Share this post


Link to post
Share on other sites
Spaceboy

@@Luke can you comment on how post processing would work if the show was being watched, either as an in progress or complete recording? i.e, would it delete the file when it completes as instructed even if someone is watching it?

Share this post


Link to post
Share on other sites
Luke

Would what delete it? I don't quite understand, sorry.

Share this post


Link to post
Share on other sites
PenkethBoy

@@Luke

 

We are trying to determine when the Post Processing event is triggered.

 

At the end of recording?

At the point where the recording has completed and the user has finished watching - so the ts file is free to be processed?

Or some other point?

Share this post


Link to post
Share on other sites
Luke

At the end of the recording. It does not take into consideration if anyone is watching.

Share this post


Link to post
Share on other sites
PenkethBoy

Thanks Good to know

Share this post


Link to post
Share on other sites
Kramerika

Your script appears to be working with Handbrake.  It did create a log as well.  Thanks!

Share this post


Link to post
Share on other sites
PenkethBoy

Remember if you enable -delFile it will do it anyway!

Share this post


Link to post
Share on other sites
cayars

I'd be careful with the settings you use for audio as there is no guarantee you will be getting AC3 audio in the file.  You may also not want to transcode the video but instead copy it as well depending on the format it's already in.  If your objective is to end up with H.264 and you are recording something on cable that is already in H.264 it's a redundant step that degrades quality and take a lot longer than needed.

 

It's best to check the file first to see what streams are in it, then craft the commands line passed to ffmpeg to achieve the desired affect.

 

Carlo

Share this post


Link to post
Share on other sites
PenkethBoy

Might be better if you were more specific who your post is directed at.

 

In this case its at i assume @@Kramerika and he's using Handbrake.

 

For info copying video from a usually less than perfect LiveTV stream - usually results in a failed conversion - not always but a high proportion of the time.

 

Each user will have different mileage - depending on quality of source stream - so one size does not fit all and a full re-encode may be necessary.

 

Checking the ts prior to processing is what i do with my more complex script - i did not post that here because i dont want to support that - and the script in the OP is an example to get people started.

Share this post


Link to post
Share on other sites
jasonmcroy

@@PenkethBoy - I am trying to test your script manually directly from Powershell to ensure I am implementing it correctly before adding it to Emby. I am sure there is something I am not putting into your script that is causing the error in my attached log but after reading over your instructions and looking over your example I can't for the life of me figure out what I am doing wrong.

 

See the error at the end of this log:

 

.txt

 

Here is my script. The only thing I changed is the ffmpeg path:

 

convert.txt

Share this post


Link to post
Share on other sites
PenkethBoy

whats you full powershell comand line look like

Share this post


Link to post
Share on other sites
CBers

@@PenkethBoy - I am trying to test your script manually directly from Powershell to ensure I am implementing it correctly before adding it to Emby. I am sure there is something I am not putting into your script that is causing the error in my attached log but after reading over your instructions and looking over your example I can't for the life of me figure out what I am doing wrong.

 

This is what I have just used from withing a PS window:

 

.\EmbyPP.ps1 -oldVideo 'X:\Videos\Live TV\Two and a Half Men (2003)\Season 3\Two and a Half Men S03E08 That Voodoo That I Do Do.ts' -Preset superfast
  • Like 1

Share this post


Link to post
Share on other sites
PenkethBoy

@@CBers sorry - thats for the script you have :) not the one Jason is using

 

Jason - what you need to do is take the above example BUT dont add the "-preset superfast" bit as that does not apply to the script above

 

also you need to be in the console window AND have changed the directory to the PS script location for .\EmbyPP etc to work

  • Like 1

Share this post


Link to post
Share on other sites
jasonmcroy

Ha. Now I feel even more confused! lol

 

Here is what I did - I went by your instructions and created the ps1 script in the same folder where I have ffmpeg and copied your script into the ps1 file and changed the ffmpeg path. 

 

I then copied a test video file into that same folder. Then I right clicked on the ps1 script which then opens up powershell. Powershell then asks me to input the file name for the video so did and hit enter. Then it asked for the in type so I put ts and then it asked for out type and I put mkv and hit enter. It does something, closes then puts out that log file I uploaded to you. 

 

I hope your not laughing at me because I barely know what I am doing but I can follow directions normally. But outside of what you tell me to do I don't have any additional knowledge. Totally willing and excited to learn though!

 

Here is a screen shot of the powershell window up to the last command it asks me for before it does it's thing:

 

post-106-0-77217300-1537901855_thumb.png

Share this post


Link to post
Share on other sites
PenkethBoy

ok - you are not following what we said

 

open a console window for powershell - its on your start menu - windows powershell > windows powershell

 

with the window open (blue as in you screen shot)

 

now change the directory to the directory that has the ps1 file in it - as you would in any command window as it will by default start in windows\system32

 

then you can do the following

 

.\embyPP.ps1 -oldvideo "<full path to the video file inside double quotes>" -intype ts -outtype mkv

  • Like 1

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...