Home  >  

Microsoft DRM part 3

Author photo
November 17, 2009 | | Comments (1)
AddThis Social Bookmark Button

Next in my series describing my implementation of Microsoft DRM I will detail how I refined the work flow to a single process. This process is portable, can be run on any machine, so it negates the need to copy large video files across the network. The process also can be run and monitored programmatically.

In my last article I had shown how to generate a profile file from a DRM server. This profile file was initially used to DRM our live audio streams but I quickly found that because it encapsulated all of the necessary information on the DRM server I could also use it in my new work flow to DRM files on any machine.

From there I wrote a new console application using the Windows Media Encoder library and the DRM profile to transcode files to Windows Media and DRM them in one single step.


Imports WMEncoderLib
Imports System.Windows.Forms
Imports System.IO


Module Module1
    Dim WithEvents Encoder As WMEncoder
    Dim glbQuitBool As Boolean = False
    Dim baseURL As String
    Dim DRMProfilePass As String = "XXX"
    Dim DRMProfile As String = "ExportProfile.drm"


    Sub Main()
       Init()
       SearchDir(baseURL)
    End Sub

    Sub Init()
      Dim sr As StreamReader = New StreamReader("baseLoc.ini")
      Dim line As String
      line = sr.ReadLine()
      sr.Close()
      baseURL = line
    End Sub

    Function DoEncode(ByVal sourceFile As String, ByVal outputFile As String, ByVal thisProfile As WMEncProfile2, ByVal fileType As String) As Boolean
     Try
     Encoder = New WMEncoder
     Dim SrcGrpColl As IWMEncSourceGroupCollection = Encoder.SourceGroupCollection
     Dim SrcGrp As IWMEncSourceGroup = SrcGrpColl.Add("SG_1")
     Dim newFile As IWMEncFile = Encoder.File

     If fileType = "video" Then
      Dim vidFile As IWMEncVideoSource = SrcGrp.AddSource(2)
      vidFile.SetInput(sourceFile)
     End If


     Dim audFile As IWMEncAudioSource = SrcGrp.AddSource(1)

     Dim DRM As IWMDRMContentAuthor
     Dim DRMProColl As IWMDRMProfileCollection
     Dim tempDRMPro As IWMDRMProfile

     DRM = Encoder.EncoderDRMContentAuthor

     ' Retrieve the collection of DRM profiles.
     DRMProColl = DRM.DRMProfileCollection

     'First remove any DRM Profiles that may still linger 
     tempDRMPro = DRMProColl.Item(0)
     DRM.RemoveDRMProfile(tempDRMPro.ID)

     ' Import a DRM profile.
     DRM.ImportDRMProfile(DRMProfilePass, DRMProfile)
     
     DRMProColl = DRM.DRMProfileCollection
     Dim DRMPro As IWMDRMProfile
     DRMPro = DRMProColl.Item(0)

     ' Set the current DRM profile into the encoding session.
     Dim vKeyID As Object = "XXXXX"
     DRM.SetSessionDRMProfile(DRMPro.ID, vKeyID)

     Dim newProfile As WMEncProfile2 = New WMEncProfile2
     audFile.SetInput(sourceFile)
     newFile.LocalFileName = outputFile
     newProfile = thisProfile

     ' newProfile.LoadFromFile(thisProfile)
     SrcGrp.Profile = newProfile
     Encoder.AutoStop = True
     Encoder.RemoteAdmin = True
     Encoder.PrepareToEncode(True)
     Encoder.Start()

     Dim x As Integer
     
     'HACK - needs this to actually work, bug in WM SDK
     For x = 0 To 30
       Application.DoEvents()
       x = x + 1
     Next


     While Encoder.RunState = WMENC_ENCODER_STATE.WMENC_ENCODER_RUNNING
       Dim temp = Encoder.RunState
       Dim y As Integer
       For y = 0 To 300
         y = y + 1
       Next
      Application.DoEvents()
     End While

     Encoder.Stop()
     Return True

     Catch ex As Exception
     Console.WriteLine(ex)
     Dim errFile As String = outputFile + ".error"
     File.Create(errFile)
     File.Delete(outputFile)
     Return False
     End Try

 Encoder = Nothing

    End Function



    Function RetrieveProfile(ByVal nFile) As WMEncProfile2
 Try
     Dim BasicEdit As WMEncBasicEdit
     BasicEdit = New WMEncBasicEdit

     ' Specify the input, output, and configuration files.
     BasicEdit.MediaFile = nFile

     ' Retrieve the duration of the input file, in seconds.
     Dim lFileDur As Long
     lFileDur = BasicEdit.Duration / 1000

     ' Retrieve the profile used by the input file.
     Dim Pro As WMEncProfile2
     Pro = BasicEdit.Profile

     Return Pro
 Catch ex As Exception
     Console.WriteLine(ex)
 End Try


    End Function

    Sub SearchDir(ByVal dir)
    'Environment.CurrentDirectory
     Dim di As New DirectoryInfo(dir)
     ' Create an array representing the files in the current directory.
     Dim fi As FileInfo() = di.GetFiles()
     Dim tempFile As FileInfo
     For Each tempFile In fi
       Dim currentProfile As WMEncProfile2
       Dim currentSourceFile As String = baseURL + "\" + tempFile.Name
       Dim currentOutputFile As String = baseURL + "\DRMed\" + tempFile.Name  
       Dim currentFType As String
       Dim currentFTypeArr = Split(tempFile.Name, ".")
       If currentFTypeArr(1) = "wma" Then
          currentFType = "audio"
       ElseIf currentFTypeArr(1) = "wmv" Then
          currentFType = "video"
       End If

       currentProfile = RetrieveProfile(currentSourceFile)
       DoEncode(currentSourceFile, currentOutputFile, currentProfile, currentFType)
     Next tempFile

    End Sub


End Module

Read more from Tom Barker. Tom Barker's Atom feed

Comments

1 Comments

fhtryr said:

If you need remove drm protection,you can use the tool.I think it is a very good tool.

Leave a comment


Tag Cloud

iPad

What's your take on the iPad? (Putting aside the Flash/iPad flame war)

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.