Playing Movie Using PowerShell (PowerShell Video Player)

Hufh….!!
I just bored with my daily activities and I decided to make some fun with powershell.
Now, We will try to playing Movie using PowerShell.
I used WPF library + XAML for that. so, let’s see it 😀

#WPF Library for Playing Movie and some components
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.ComponentModel
#XAML File of WPF as windows for playing movie
[xml]$XAML = @"

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PowerShell Video Player" Height="355" Width="553" ResizeMode="NoResize">
    <Grid Margin="0,0,2,3">
        <MediaElement Height="250" Width="525" Name="VideoPlayer" LoadedBehavior="Manual" UnloadedBehavior="Stop" Margin="8,10,10,61" />
        <Button Content="Pause" Name="PauseButton" HorizontalAlignment="Left" Margin="236,283,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Play" Name="PlayButton" HorizontalAlignment="Left" Margin="236,283,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>
"@

#Movie Path
[uri]$VideoSource = "C:UsersRakaDownloadsHiVi!  - Orang Ke-3 Official MV HD.mp4"

#Devide All Objects on XAML
$XAMLReader=(New-Object System.Xml.XmlNodeReader $XAML)
$Window=[Windows.Markup.XamlReader]::Load( $XAMLReader )
$VideoPlayer = $Window.FindName("VideoPlayer")
$PauseButton = $Window.FindName("PauseButton")
$PlayButton = $Window.FindName("PlayButton")

#Video Default Setting
$VideoPlayer.Volume = 100;
$VideoPlayer.Source = $VideoSource;
$VideoPlayer.Play()
$PauseButton.Visibility = [System.Windows.Visibility]::Visible
$PlayButton.Visibility = [System.Windows.Visibility]::Hidden

#Button click event 
$PlayButton.Add_Click({
    $VideoPlayer.Play()
    $PauseButton.Visibility = [System.Windows.Visibility]::Visible
    $PlayButton.Visibility = [System.Windows.Visibility]::Hidden
})
$PauseButton.Add_Click({
    $VideoPlayer.Pause()
    $PauseButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayButton.Visibility = [System.Windows.Visibility]::Visible
})

#Show Up the Window 
$Window.ShowDialog() | out-null

Here’s a Screenshot of PowerShell Video Player
PowerShell Movie Player

I knew that it’s not good enough, but not too bad 😛
I will create new version of it (if i need to…. hahaha)
LOL!! PowerShell is so much fun!!

Have fun with it!!

Raka Satria

4 comments on “Playing Movie Using PowerShell (PowerShell Video Player)

  1. Howard says:

    Wed 4Sep2013
    From hnr / ausgeni@hotmail.com.au
    Dear Raka Satria,

    Thank you for posting the ” playing-movie-using-powershell”.
    This gave me the start I needed to configure a VideoPlayer. I have added more options to your code-base and have achieved my desired result.
    My function code for “Show-WPFVideoPlayer” is included here.
    Three of my library functions are included with this module. These functions are called from within “Show-WPFVideoPlayer”.
    The videofile in C:\Users\Public\Videos has been used for testing.

    This function is based on my Building Block Approach. Presently I am unable to get the VideoPlayer working in a codeonly mode, ie without XAML.
    I hear the sound but do not see the video. More effort from me is required here.

    Regards,
    Howard.
    # ==============================================================================
    function Show-WPFVideoPlayer ([string]$Background, [bool]$CenterScreen, [double]$LocationX, [double]$LocationY, [bool]$NaturalVideoHeight, [double]$Height, [string]$VideoFileLocation)
    # Aug2013 – hnr / ausgeni
    # MSDN reference: System.Windows Namespace
    # http://msdn.microsoft.com/en-us/library/system.windows.aspx
    # Window Class
    # http://msdn.microsoft.com/en-us/library/system.windows.window.aspx
    # MediaElement Class
    # http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-4
    # XAML Namespaces and Namespace Mapping for WPF XAML
    # http://msdn.microsoft.com/en-us/library/ms747086.aspx
    # Wed 21Aug2013
    # This post has helped me with this project ; I thank Raka Satria
    # reference: https://rakatechblog.wordpress.com/

    # Background = Window Background
    # CenterScreen = true – position Window in centre of screen
    # any values for LocationX and LocationY are disregarded ; LocationX and LocationY should be set to 0.0
    # false – use LocationX and LocationY to position Window
    # LocationX = pixels from Top Left Corner of Screen of the Top Left Hand Corner of VideoPlayer
    # LocationY = pixels from Top Left Corner of Screen of the Top Left Hand Corner of VideoPlayer
    # set CenterScreen to false
    # NaturalVideoHeight = true – Video is shown at the Natural Video Height
    # any value for Height is disregarded ; Height should be set to 0.0
    # false – use value for Height
    # Height = pixels of the height of the VideoPlayer
    # Minimum Height = 131 pixels
    # set NaturalVideoHeight to false
    # VideoFileLocation = absolute path of the VideoFile to play ; ensure that the VideoFile exists as no check is performed in this function
    # the string is converted to a uri
    {
    # —– Add some parameters to build the window ——————————–

    [double] $local:Margin = 2.0
    [double] $local:ButtonControlWidth = 75.0
    [double] $local:ButtonControlHeight = 24.0
    [double] $local:MinWindowWidth = (3.0*($ButtonControlWidth + $Margin) – $Margin) # the width of the Control Buttons array

    if ($NaturalVideoHeight -eq $true) { $Height = 131.0 } # WindowWidth and WindowHeight are recomputed after media loading has finished
    [double] $local:WindowHeight = $Height + (2.0*$Margin) + $ButtonControlHeight
    [double] $local:WindowWidth = $Height * (16.0/9.0)

    if ($CenterScreen -eq $true) { $Res = Get-ScreenResolution
    $LocationX = (($Res[0] – $WindowWidth) / 2)
    $LocationY = (($Res[1] – $WindowHeight) / 2)
    }
    [double] $local:ButtonControlLocX1 = (($WindowWidth – $MinWindowWidth) / 2.0)
    [double] $local:ButtonControlLocX2 = $ButtonControlLocX1 + $ButtonControlWidth + $Margin
    [double] $local:ButtonControlLocX3 = $ButtonControlLocX2 + $ButtonControlWidth + $Margin
    [double] $local:ButtonControlLocY = $Height + $Margin

    # —– Check video file type ————————————————–

    [string] $local:FileTypes = “mpg mp4 wmv”
    [string] $local:FileExt = $null
    $FileExt = $VideoFileLocation.Substring( $VideoFileLocation.LastIndexOf(“.”)+1 )
    if ( ($FileTypes).Contains($FileExt) -ne $true )
    {
    $Message = “ERROR: WPF Video Player does not suuport this video file type.`nUse only .mpg, .mp4, .wav video file types”
    $Result = Show-MessageBox -Message $Message -Caption “WPF Video Player” -Buttons “OK” -Icon “Error”
    return
    }

    if ( $WindowWidth -lt $MinWindowWidth )
    {
    $Message = “ERROR: Video Player Height is too small.`nIncrease Height to above 129 pixels”
    $Result = Show-MessageBox -Message $Message -Caption “WPF Video Player” -Buttons “OK” -Icon “Error”
    return
    }
    # —– Build the window ——————————————————-

    #WPF Library for Playing Movie and some components
    Add-Type -AssemblyName PresentationFramework
    ### Add-Type -AssemblyName System.ComponentModel # gives error
    #XAML File of WPF as windows for playing movie

    [xml]$XAML = @”

    “@

    # System.Windows.Markup.XamlReader

    #Devide All Objects on XAML
    $XAMLReader=(New-Object System.Xml.XmlNodeReader $XAML)
    $Window=[Windows.Markup.XamlReader]::Load( $XAMLReader )

    $VideoPlayer = $Window.FindName(“VideoPlayer”)
    $PauseButton = $Window.FindName(“PauseButton”)
    $PlayButton = $Window.FindName(“PlayButton”)
    $PlayAgainButton = $Window.FindName(“PlayAgainButton”)
    $StopButton = $Window.FindName(“StopButton”)
    $CloseButton = $Window.FindName(“CloseButton”)

    #Video Default Setting
    $VideoPlayer.Source = $VideoFileLocation # string is converted to a uri
    $VideoPlayer.Play()
    $PauseButton.Visibility = [System.Windows.Visibility]::Visible
    $PlayButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayAgainButton.Visibility = [System.Windows.Visibility]::Hidden
    $StopButton.Visibility = [System.Windows.Visibility]::Visible
    $CloseButton.Visibility = [System.Windows.Visibility]::Visible

    # —– Add event handlers —————————————————–

    $Window.Add_KeyUp({
    if ($_.KeyCode -eq “Escape”) { $VideoPlayer.Stop() ; $Window.Close() }
    })

    $PlayButton.Add_Click({
    $VideoPlayer.Play()
    $PauseButton.Visibility = [System.Windows.Visibility]::Visible
    $PlayButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayAgainButton.Visibility = [System.Windows.Visibility]::Hidden
    $StopButton.Visibility = [System.Windows.Visibility]::Visible
    })
    $PlayAgainButton.Add_Click({
    $VideoPlayer.Source = $VideoFileLocation
    $Window.Background = $Background
    $VideoPlayer.Play()
    $PauseButton.Visibility = [System.Windows.Visibility]::Visible
    $PlayButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayAgainButton.Visibility = [System.Windows.Visibility]::Hidden
    $StopButton.Visibility = [System.Windows.Visibility]::Visible
    })
    $PauseButton.Add_Click({
    $VideoPlayer.Pause()
    $PauseButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayButton.Visibility = [System.Windows.Visibility]::Visible
    $PlayAgainButton.Visibility = [System.Windows.Visibility]::Hidden
    })
    $StopButton.Add_Click({
    $VideoPlayer.Stop()
    $VideoPlayer.Source = $null # clear the video area
    $Window.Background = “Black”
    $StopButton.Visibility = [System.Windows.Visibility]::Hidden
    $PauseButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayAgainButton.Visibility = [System.Windows.Visibility]::Visible
    })
    $CloseButton.Add_Click({
    $VideoPlayer.Stop()
    $Window.Close()
    })
    # Occurs when the media has ended
    $VideoPlayer.Add_MediaEnded({
    # $VideoPlayer.Source = $null # clear the video area
    # $Window.Background = “Black” # color the video area
    $StopButton.Visibility = [System.Windows.Visibility]::Hidden
    $PauseButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayAgainButton.Visibility = [System.Windows.Visibility]::Visible
    })
    # —– Occurs when media loading has finished ———————————
    # check if player is to be at NaturalVideoHeight
    # reset the window width and height ; reset the location of the control buttons

    $VideoPlayer.Add_MediaOpened({
    # DEBUG
    # $a = $VideoPlayer.NaturalVideoHeight ; Write-Host “NaturalVideoHeight= $a”
    # $a = $VideoPlayer.NaturalVideoWidth ; Write-Host “NaturalVideoWidth= $a”
    # $a = $VideoPlayer.IsLoaded ; Write-Host “IsLoaded= $a”

    if ($NaturalVideoHeight -eq $true)
    {
    $WindowWidth = $VideoPlayer.NaturalVideoWidth
    $WindowHeight = $VideoPlayer.NaturalVideoHeight + (2.0*$Margin) + $ButtonControlHeight
    $Window.Width = $WindowWidth
    $Window.Height = $WindowHeight

    $VideoPlayer.Height = $VideoPlayer.NaturalVideoHeight

    $ButtonControlLocX1 = (($WindowWidth – $MinWindowWidth) / 2.0)
    $ButtonControlLocX2 = $ButtonControlLocX1 + $ButtonControlWidth + $Margin
    $ButtonControlLocX3 = $ButtonControlLocX2 + $ButtonControlWidth + $Margin
    $ButtonControlLocY = $VideoPlayer.NaturalVideoHeight + $Margin

    $PauseButton.Margin = [System.Windows.Thickness](“$ButtonControlLocX1,$ButtonControlLocY,0,0”)
    $PlayButton.Margin = “$ButtonControlLocX1,$ButtonControlLocY,0,0”
    $PlayAgainButton.Margin = “$ButtonControlLocX1,$ButtonControlLocY,0,0”
    $StopButton.Margin = “$ButtonControlLocX2,$ButtonControlLocY,0,0”
    $CloseButton.Margin = “$ButtonControlLocX3,$ButtonControlLocY,0,0”

    if ($CenterScreen -eq $true) { $Res = Get-ScreenResolution
    $LocationX = (($Res[0] – $Window.Width) / 2)
    $LocationY = (($Res[1] – $Window.Height) / 2)
    $Window.Left = $LocationX
    $Window.Top = $LocationY
    }
    }
    })
    # ——————————————————————————
    $VideoPlayer.Add_MediaFailed({ Write-Host “Media Failed Event fired” }) # could write a MessageBox here
    # ——————————————————————————
    [void] $Window.ShowDialog()
    }
    # ==============================================================================
    function Get-ScreenResolution
    # hnr / ausgeni Apr2013
    # Gets the physical screen resolution in x and y pixels of the users screen
    # Using WMI – $screen = Get-WmiObject -class Win32_DesktopMonitor
    # $screen.ScreenWidth
    # $screen.ScreenHeight
    # Using .NET – In a multi monitor environment, get info for all monitors
    # [System.Windows.Forms.Screen]::AllScreens
    # Bounds returns the total area of the display
    # WorkingArea returns the desktop bounds, excluding such things as taskbars, toolbars, etc
    {
    [array] $Res = New-Object ‘int16[]’ 4

    Add-NETNamespace -NETNamespace “System.Windows.Forms”
    $Res[0] = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Size.Width
    $Res[1] = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Size.Height

    $Res[2] = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Size.Width
    $Res[3] = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Size.Height
    return $Res
    }
    # ==============================================================================
    function Show-MessageBox ([string] $Message, [string] $Caption, [string] $Buttons, [string] $Icon)
    # hnr / ausgeni Apr2013
    # Displays a message box that can contain text, buttons, and symbols that inform and instruct the user
    # MSDN reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx
    # http://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxbuttons.aspx
    # http://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxicon.aspx

    # Message = text to display in the message box
    # Caption = text to display in the title bar of the message box
    # Buttons = buttons to display in the message box
    # Icon = icon to display in the message box
    # MSDN reference: MessageBoxIcon Enumeration (System.Windows.Forms)
    # error – a white X in a circle with a red background
    # warning – an exclamation point in a triangle with a yellow background
    # information – a lowercase letter i in a circle
    {
    [string] $MBResult = $null
    Add-NETNamespace -NETNamespace “System.Windows.Forms”

    Set-CursorPosition -PositionX 0 -PositionY 0 -CenterScreen $true
    $MBButtons = [System.Windows.Forms.MessageBoxButtons]::$Buttons
    $MBIcon = [System.Windows.Forms.MessageBoxIcon]::$Icon
    $MBResult = [System.Windows.Forms.MessageBox]::Show($Message, $Caption, $MBButtons, $MBIcon)
    return $MBResult
    }
    # ==============================================================================
    function Add-NETNamespace ([string] $NETNamespace)
    # hnr / ausgeni Apr2013
    # reference: http://learningpcs.blogspot.com.au/2012/06/powershell-v2-test-if-assembly-is.html#!/2012/06/powershell-v2-test-if-assembly-is.html
    {
    # Check if the needed assembly is loaded
    if( ( [System.AppDomain]::CurrentDomain.GetAssemblies() | Where {$_ -match “$NETNamespace”} ) -eq $null )
    {
    Add-Type -AssemblyName $NETNamespace
    }
    }
    # ==============================================================================
    cls
    # test some locations and heights on the Video Player

    # NaturalVideoHeight=720
    [string] $VideoFileLocation = “C:\Users\Public\Videos\Sample Videos\Wildlife.wmv”

    [string] $Background = “Transparent”
    [double] $VPLocX = 200.0
    [double] $VPLocY = 50.0
    [double] $VPHeight = 288.0
    Show-WPFVideoPlayer -Background $Background -CenterScreen $false -LocationX $VPLocX -LocationY $VPLocY -NaturalVideoHeight $false -Height $VPHeight -VideoFileLocation $VideoFileLocation

    [string] $Background = “Black”
    [double] $VPLocX = 800.0
    [double] $VPLocY = 50.0
    [double] $VPHeight = 192.0
    Show-WPFVideoPlayer -Background $Background -CenterScreen $false -LocationX $VPLocX -LocationY $VPLocY -NaturalVideoHeight $false -Height $VPHeight -VideoFileLocation $VideoFileLocation

    [string] $Background = “Transparent”
    [double] $VPLocX = 300.0
    [double] $VPLocY = 400.0
    [double] $VPHeight = 129.0
    Show-WPFVideoPlayer -Background $Background -CenterScreen $false -LocationX $VPLocX -LocationY $VPLocY -NaturalVideoHeight $false -Height $VPHeight -VideoFileLocation $VideoFileLocation

    ##[string] $Background = “Transparent”
    ##[double] $VPLocX = 300.0
    ##[double] $VPLocY = 150.0
    ##[double] $VPHeight = 120.0
    ##Show-WPFVideoPlayer -Background $Background -CenterScreen $false -LocationX $VPLocX -LocationY $VPLocY -NaturalVideoHeight $false -Height $VPHeight -VideoFileLocation $VideoFileLocation

    [string] $Background = “LightCyan”
    [double] $VPLocX = 650.0
    [double] $VPLocY = 250.0
    [double] $VPHeight = 384.0
    Show-WPFVideoPlayer -Background $Background -CenterScreen $false -LocationX $VPLocX -LocationY $VPLocY -NaturalVideoHeight $false -Height $VPHeight -VideoFileLocation $VideoFileLocation

    # test CenterScreen on the Video Player

    [string] $Background = “Transparent”
    [double] $VPHeight = 288.0
    Show-WPFVideoPlayer -Background $Background -CenterScreen $true -LocationX 0.0 -LocationY 0.0 -NaturalVideoHeight $false -Height $VPHeight -VideoFileLocation $VideoFileLocation

    # test CenterScreen and NaturalVideoHeight on the Video Player

    [string] $Background = “Transparent”
    Show-WPFVideoPlayer -Background $Background -CenterScreen $true -LocationX 0.0 -LocationY 0.0 -NaturalVideoHeight $true -Height 0.0 -VideoFileLocation $VideoFileLocation

    # ——————————————————————————

    • Raka Satria says:

      I have learned your script and i found this
      [xml]$XAML = @”

      “@

      How can you create windows and find button if you’re not define them in your script ?

    • Raka Satria says:

      # —– Build the window ——————————————————-
      #WPF Library for Playing Movie and some components
      Add-Type -AssemblyName PresentationFramework
      Add-Type -AssemblyName System.ComponentModel #MVC must be installed
      #XAML File of WPF as windows for playing movie
      #[xml]$XAML = “”
      # System.Windows.Markup.XamlReader

      #Devide All Objects on XAML
      #$XAMLReader=(New-Object System.Xml.XmlNodeReader $XAML)
      #$Window=[Windows.Markup.XamlReader]::Load( $XAMLReader )
      #$VideoPlayer = $Window.FindName(“VideoPlayer”)
      #$PauseButton = $Window.FindName(“PauseButton”)
      #$PlayButton = $Window.FindName(“PlayButton”)
      #$PlayAgainButton = $Window.FindName(“PlayAgainButton”)
      #$StopButton = $Window.FindName(“StopButton”)
      #$CloseButton = $Window.FindName(“CloseButton”)

      $Window =New-Object System.Windows.Window
      $VideoPlayer = New-Object System.Windows.Controls.MediaElement
      $PauseButton = New-Object System.Windows.Controls.Button
      $PlayButton = New-Object System.Windows.Controls.Button
      $PlayAgainButton = New-Object System.Windows.Controls.Button
      $StopButton = New-Object System.Windows.Controls.Button
      $CloseButton = New-Object System.Windows.Controls.Button

      Pleace change you code like above and complete their properties

  2. Hello, is it possible to play a video on windows 10 for IoT? in this case, Raspberry Pi?
    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s