Announcement

Collapse
No announcement yet.

File renamer - Power Automate Desktop flow

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    StreamFab for Windows File renamer - Power Automate Desktop flow

    While StreamFab has a Template for file names and such, it doesn't allow for all the options I want, and sometimes gets the resolution wrong. This Power Automate Desktop script uses MediaInfo CLI to capture the parameters of the file and sets variables that will be used in the file renaming process.

    For my plex library I like to use the Edition tags, and capture what service the file was downloaded from, if its 4K and if it has HDR or not. I also use abbreviations for the services.

    This flow meets my needs, and works, I've been using this for a long time, so I thought I'd share. I know there are other renamers out there, but I use Power Automate (cloud) for work so thought I'd play with Power Automate Desktop.

    The script evaluates for video resolution of 2160, 1080, 720. It assumes progressive scan as everything I've downloaded has been progressive. It also evaluates HDR types of HDR10, DV, or DoVi HDR10 (which is DV with fall back to HDR10 included). Audio is evaluated for EAC3, EAC3 JOC, and AAC.


    This script relies on StreamFab's VIP Services naming template for MOVIES to be:
    <moviename> (<year>) {edition-}

    Note: Peacock doesn't use StreamFab's name template currently, so I have separate rename steps for those files.


    This script only looks at the top level of the Service Provider folder, it does not recurse subfolders. It does not touch already processed movies or touch TV shows as they would be in subfolders.

    I have StreamFab set to download as MKV, so this script is set to work only on MKV files. If you download as MP4, you would need to make a few changes in the flow, where MKV is specifically called out (like steps 37, and 95-98)

    What to expect:

    Files downloaded at StreamFabOut\Amazon\MovieName (year) {edition-}.mkv will be renamed to StreamFabOut\Amazon\MovieName (year)\MovieName (year) 1080p {edition-Amz DL}.mkv

    Similarly a 4K movie from Max with only HDR10, StreamFabOut\Max\MovieName (year) {edition-}.mkv will be renamed to StreamFabOut\Max\MovieName (year)\MovieName (year) 2160p {edition-Max DL 4K HDR10}.mkv

    When copied to your Plex movie folder, they will show as the movie name, and the edition tag will be respected.


    REQUIRED:
    • Power Automate Desktop -> https://learn.microsoft.com/en-us/po...-flows/install
    • MediaInfo CLI from -> https://mediaarea.net/en/MediaInfo/Download/Windows
      • ONE TIME SETUP: With Power Automate Desktop Closed, download MediaInfo CLI, then unzip it, (move the folder if you need to) and add the folder to the Windows Path environment variable. To add to Path... Windows Settings -> System -> About -> Advanced System Settings -> Environment Variables. In the SYSTEM variables section, select Path, click the Edit Button, and add the folder where you unzipped MediaInfo CLI.
    • Select this entire script in Post #2 of this thread, copy to the clipboard. Open Power Automate Desktop, Create a New Flow, name it, click Create. In the Main section (middle of screen), right click and paste. The flow steps will be created there. (if copy/paste of the "code" doesn't work, please see the attached text file, to copy/paste from)
    • Make sure you set the FolderBase variable (lines 3-8 of the flow) in the Flow to match the top level if your StreamFab output folder. (also see personalization note below)

    Personalization notes:
    • I have both a laptop and desktop, the location of the download folders are different on each computer. Therefore I have a two sets of IF statements (rows 3 through 8) to evaluate if the folder exists and sets FolderBase = to the correct StreamFab output folder for the computer I'm working on.
    • While I do have MediaInfo CLI capture video and audio details, I only use the video info when renaming. You can adjust rows 99-115 to include the Audio variable if you wish.
    • Lines 15-34 set the custom Service names or abbreviations used when renaming the files. So if you don't like Amz for Amazon, edit line 16 for example.
    • You don't need to have the edition tag in the final file name, You can tweak the output file name string and variables on rows 99-115. ​

    #2
    Code:
    /# <<Expand and read this entire comment!!>>
    
    This script is the initial attempt at making the Flow more generic to use for MOVIES from all services.  It relies on StreamFab using a folder for each service (See the naming template below)
    
    REQUIRED:  Download MediaInfo CLI from -> https://mediaarea.net/en/MediaInfo/Download/Windows   ONE TIME SETUP:  unzip it, and add the folder to the Windows Path environment variable, exit and restart Power Automate before running or HDR/DV detection will not work.
    
    This script relies on StreamFab's VIP Services naming template for MOVIES be:
    <moviename> <year> {edition-}
    
    Make sure you set the FolderBase line 4 and 7.   The IF statements are there (lines 3-8) in case you have more than one computer.
    Folder Base =StreamFab's Output folder.
    
    
    
    
    
    
    
    #/
    DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateAndTime CurrentDateTime=> DT_Start
    IF (Folder.IfFolderExists.Exists Path: $'''F:\\BluRay-Working\\- DVDFab10\\DVDFab Downloader''') THEN
        SET FolderBase TO $'''F:\\BluRay-Working\\- DVDFab10\\DVDFab Downloader'''
    END
    IF (Folder.IfFolderExists.Exists Path: $'''C:\\DVDFab\\StreamFab\\Download''') THEN
        SET FolderBase TO $'''C:\\DVDFab\\StreamFab\\Download'''
    END
    Folder.GetSubfolders Folder: FolderBase FolderFilter: $'''*''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Subfolders=> Folders
    SET MovieCount TO 0
    LOOP FOREACH CurrentItem IN Folders
        **REGION Configure Startup Variables
        SET FolderPath TO CurrentItem
        DISABLE SET FolderPath TO $'''%FolderBase%\\%FolderService%'''
        IF CurrentItem.Name = $'''Amazon''' THEN
            SET Service TO $'''Amz'''
        END
        IF CurrentItem.Name = $'''AppleTV+''' THEN
            SET Service TO $'''A+'''
        END
        IF CurrentItem.Name = $'''Disney+''' THEN
            SET Service TO $'''D+'''
        END
        IF CurrentItem.Name = $'''Hulu''' THEN
            SET Service TO $'''Hulu'''
        END
        IF CurrentItem.Name = $'''Max''' THEN
            SET Service TO $'''Max'''
        END
        IF CurrentItem.Name = $'''netflix''' THEN
            SET Service TO $'''Netflix'''
        END
        IF CurrentItem.Name = $'''Peacock''' THEN
            SET Service TO $'''Peacock'''
        END
        **ENDREGION
        Folder.GetFiles Folder: FolderPath FileFilter: $'''*.mkv''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> FilesMKV
        LOOP FOREACH MP4Files IN FilesMKV
            SET Is4K TO $'''No'''
            File.GetPathPart File: MP4Files Directory=> Directory FileName=> FileName FileNameWithoutExtension=> FileNameNoExtension Extension=> FileExtension
            **REGION Get Video info - MediaInfo CLI
            # This DOS Command runs MediaInfoCLI to return Frame Width, Frame Height, HDR Format and Video Codec used.   Each are then stored in variables for use later.
            @@copilotGeneratedAction: 'False'
    Scripting.RunDOSCommand.RunDOSCommand DOSCommandOrApplication: $'''mediainfo --Output=Video;%%Width%%x%%Height%%x%%HDR_Format/String%%x%%Format%% \"%Directory%\\%FileNameNoExtension%.mkv\"''' WorkingDirectory: Directory StandardOutput=> CommandOutput2 StandardError=> CommandErrorOutput2 ExitCode=> CommandExitCode2
            Text.SplitText.SplitWithDelimiter Text: CommandOutput2 CustomDelimiter: $'''x''' IsRegEx: False Result=> MediaInfoOut
            SET varFrameWidth TO MediaInfoOut[0]
            SET varFrameHeight TO MediaInfoOut[1]
            SET HDR_Format TO MediaInfoOut[2]
            SET CodecID TO MediaInfoOut[3]
            # This DOS Command returns the Audio Format which is stored as a variable for use later
            @@copilotGeneratedAction: 'False'
    Scripting.RunDOSCommand.RunDOSCommand DOSCommandOrApplication: $'''mediainfo --Output=Audio;%%Format/Info%% \"%Directory%\\%FileNameNoExtension%.mkv\"''' WorkingDirectory: Directory StandardOutput=> AudioFormat StandardError=> CommandErrorOutput3 ExitCode=> CommandExitCode3
            **ENDREGION
            **REGION Determine Audio Type based on Audio Format string returned from MediaInfo CLI
            # This Section evaluates the string returned from MediaInfo CLI for Audio type looking for EAC3, EAC3 JOC, or AAC and sets a variable to optionally be used in file naming later.
            SET Audio TO $'''%''%'''
            IF Contains(AudioFormat, $'''Enhanced AC-3''', True) THEN
                SET Audio TO $''' E-AC-3'''
            END
            IF Contains(AudioFormat, $'''Enhanced AC-3 with Joint Object Coding''', True) THEN
                SET Audio TO $''' Atmos'''
            END
            IF Contains(AudioFormat, $'''Advanced Audio Codec Low Complexity''', True) THEN
                SET Audio TO $''' AAC'''
            END
            **ENDREGION
            **REGION Determine video resolution
            IF (varFrameHeight = 2160 OR varFrameWidth = 3840) = $'''True''' THEN
                SET Resolution TO $'''2160p'''
            END
            IF (varFrameWidth = 1920 OR varFrameHeight = 1080) = $'''True''' THEN
                SET Resolution TO $'''1080p'''
            END
            IF (varFrameWidth = 1280 OR varFrameHeight = 720) = $'''True''' THEN
                SET Resolution TO $'''720p'''
            END
            **ENDREGION
            **REGION Set HDRType - Mediainfo CLI
            IF HDR_Format = $'''SMPTE ST 2086, HDR10 compatible''' THEN
                SET HDRType TO $'''HDR10'''
            END
            IF (Contains(HDR_Format, 'dvhe.08.06', True) OR Contains(HDR_Format, 'dvhe.07.06', True)) = $'''True''' THEN
                SET HDRType TO $'''DoVi HDR10'''
            END
            IF Contains(HDR_Format, $'''dvhe.05.06''', True) THEN
                SET HDRType TO $'''DV'''
            END
            **ENDREGION
            DISABLE GOTO 'End'
            IF (Service = 'Peacock') = $'''True''' THEN
                DISABLE SET NewVar3 TO MP4Files.NameWithoutExtension
                Text.SplitText.SplitWithDelimiter Text: MP4Files.NameWithoutExtension CustomDelimiter: $'''.mkv''' IsRegEx: True Result=> TextList
            ELSE
                DISABLE SET NewVar3 TO FileName
                Text.SplitText.SplitWithDelimiter Text: FileName CustomDelimiter: $'''{e''' IsRegEx: True Result=> TextList
            END
            SET MKVFilesFullname TO $'''%Directory%\\%MP4Files.NameWithoutExtension%.mkv'''
            SET MKVFileName TO $'''%MP4Files.NameWithoutExtension%.mkv'''
            SET MKVFilesFullname TO $'''%Directory%\\%MP4Files.NameWithoutExtension%.mkv'''
            SET MKVFilesFullname1 TO $'''%Directory%\\%MP4Files.NameWithoutExtension%'''
            IF (Service = 'Peacock') = $'''True''' THEN
                IF Resolution = $'''2160p''' THEN
                    File.RenameFiles.RenameAddText Files: MP4Files.FullName TextToAdd: $''' %Resolution% {edition-%Service% DL 4K %HDRType%}''' TextPosition: File.AddTextPosition.AfterName IfFileExists: File.IfExists.Overwrite RenamedFiles=> RenamedFiles
                    SET NewFolderName1 TO $'''%TextList[0]%- 4K'''
                ELSE
                    File.RenameFiles.RenameAddText Files: MP4Files.FullName TextToAdd: $''' %Resolution% {edition-%Service% DL}''' TextPosition: File.AddTextPosition.AfterName IfFileExists: File.IfExists.DoNothing RenamedFiles=> RenamedFiles
                    SET NewFolderName1 TO TextList[0]
                END
            ELSE
                IF Resolution = $'''2160p''' THEN
                    File.RenameFiles.RenameReplaceText Files: MKVFilesFullname TextToReplace: $'''{edition-}''' ReplaceWith: $'''%Resolution% {edition-%Service% DL 4K %HDRType%}''' IfFileExists: File.IfExists.DoNothing RenamedFiles=> RenamedFiles
                    SET NewFolderName1 TO $'''%TextList[0]%- 4K'''
                ELSE
                    File.RenameFiles.RenameReplaceText Files: MKVFilesFullname TextToReplace: $'''{edition-}''' ReplaceWith: $'''%Resolution% {edition-%Service% DL}''' IfFileExists: File.IfExists.DoNothing RenamedFiles=> RenamedFiles
                    SET NewFolderName1 TO TextList[0]
                END
            END
            Folder.Create FolderPath: FolderPath FolderName: NewFolderName1 Folder=> NewFolder
            SET NewRenamed TO RenamedFiles[0]
            DISABLE File.Move Files: NewRenamed.FullName Destination: NewFolder.FullName IfFileExists: File.IfExists.DoNothing MovedFiles=> MovedFiles
            # since PAD's File Move/File Copy funtions weren't working,  I went with DOS command instead.    These next two steps handle that.
            SET DOSCommand TO $'''move \"%NewRenamed%\"  \"%NewFolder%\"'''
            Scripting.RunDOSCommand.RunDOSCommand DOSCommandOrApplication: DOSCommand WorkingDirectory: FolderPath StandardOutput=> CommandOutput StandardError=> CommandErrorOutput ExitCode=> CommandExitCode
            Variables.IncreaseVariable Value: MovieCount IncrementValue: 1
        END
    END
    DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateAndTime CurrentDateTime=> DT_End
    DateTime.Subtract FromDate: DT_End SubstractDate: DT_Start TimeUnit: DateTime.DifferenceTimeUnit.Hours TimeDifference=> TimeDifference
    Variables.TruncateNumber.GetIntegerPart Number: TimeDifference Result=> Hours
    Variables.TruncateNumber.GetDecimalPart Number: TimeDifference Result=> Remainder
    SET Minutes TO Remainder * 60
    Variables.TruncateNumber.GetDecimalPart Number: Minutes Result=> Remainder
    Variables.TruncateNumber.GetIntegerPart Number: Minutes Result=> Minutes
    Variables.TruncateNumber.GetIntegerPart Number: Remainder * 60 Result=> Seconds
    Display.ShowMessageDialog.ShowMessageWithTimeout Title: $'''Complete''' Message: $'''Processed %MovieCount% movies
    %Hours%h %Minutes%m %Seconds%s''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True Timeout: 180 ButtonPressed=> ButtonPressed
    ​
    ​
    Attached Files

    Comment

    Working...
    X