Jump to content


Photo

Post Processing Script - Working Example - Powershell


  • Please log in to reply
44 replies to this topic

#1 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 09 September 2018 - 03:14 AM

Having read this thread over the last couple of days https://emby.media/c...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

 

 


  • CBers, jasonmcroy, Spaceboy and 3 others like this

#2 CBers OFFLINE  

CBers

    Advanced Member

  • Moderators
  • 15014 posts
  • Local time: 08:10 AM
  • LocationKent, England.

Posted 09 September 2018 - 06:11 AM

Great work @ PenkethBoy .
  • PenkethBoy likes this

#3 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 11 September 2018 - 10:40 AM

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.


  • CBers likes this

#4 Kramerika OFFLINE  

Kramerika

    Advanced Member

  • Members
  • 178 posts
  • Local time: 03:10 AM
  • LocationPA, US of A

Posted 11 September 2018 - 10:02 PM

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?



#5 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 12 September 2018 - 09:10 AM

why are you using a bat file?


  • CBers likes this

#6 Kramerika OFFLINE  

Kramerika

    Advanced Member

  • Members
  • 178 posts
  • Local time: 03:10 AM
  • LocationPA, US of A

Posted 12 September 2018 - 04:02 PM

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



#7 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 12 September 2018 - 05:46 PM

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



#8 Kramerika OFFLINE  

Kramerika

    Advanced Member

  • Members
  • 178 posts
  • Local time: 03:10 AM
  • LocationPA, US of A

Posted 12 September 2018 - 08:01 PM

Attached File  post processing.png   87.82KB   4 downloads

 

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?



#9 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 13 September 2018 - 08:35 AM

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.



#10 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 13 September 2018 - 08:53 AM

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.


  • Kramerika likes this

#11 Spaceboy OFFLINE  

Spaceboy

    Advanced Member

  • Members
  • 3891 posts
  • Local time: 08:10 AM

Posted 13 September 2018 - 09:01 AM

@ 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?



#12 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 138123 posts
  • Local time: 03:10 AM

Posted 13 September 2018 - 11:33 AM

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

#13 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 13 September 2018 - 11:37 AM

@ 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?



#14 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 138123 posts
  • Local time: 03:10 AM

Posted 13 September 2018 - 11:40 AM

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

#15 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 13 September 2018 - 12:14 PM

Thanks Good to know



#16 Kramerika OFFLINE  

Kramerika

    Advanced Member

  • Members
  • 178 posts
  • Local time: 03:10 AM
  • LocationPA, US of A

Posted 13 September 2018 - 03:42 PM

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



#17 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 13 September 2018 - 04:03 PM

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



#18 cayars OFFLINE  

cayars

    Advanced Member

  • Alpha Testers
  • 2945 posts
  • Local time: 03:10 AM

Posted 17 September 2018 - 02:35 PM

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



#19 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 3392 posts
  • Local time: 08:10 AM
  • LocationWarrington,UK

Posted 18 September 2018 - 12:26 AM

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.



#20 jasonmcroy OFFLINE  

jasonmcroy

    Advanced Member

  • Super Testers
  • 1675 posts
  • Local time: 03:10 AM
  • LocationTampa, FL

Posted 25 September 2018 - 12:47 PM

@ 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:

 

Attached File  .txt   2KB   2 downloads

 

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

 

Attached File  convert.txt   1.76KB   6 downloads






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users