Sign in to follow this  
Followers 0
SpookMeister

Media Encoder

1 post in this topic

Well, not that it will be very usefull to any of you except perhaps as an example of one way of doing things, but here is my first semi-serious full program. Comments, questions, and critiques always welcome.

Oh, it just takes most of the user intervention out of starting an audio broadcast for my little company.

#include <ftp.au3>
#include <GUIConstants.au3>
; Overview: This script opens a default.wme file and edits it to suit
; our needs. Then it runs the encoder application in hidden mode
; and begins both broadcasting a live stream and saving a copy to a
; network drive for archival purposes. When the user stops the broadcast
; the application is closed and the archived file is FTP'd to another server

Global $fullpath, $name, $drv

GUICreate("Audio Encoder", 410, 200)
$Label_1 = GUICtrlCreateLabel("Type in a title for the broadcast and then" _
            & " click the button below", 30, 20, 300, 21, 0x00000020)
$Label_2 = GUICtrlCreateLabel("", 30, 130, 300, 21, 0x00000020)
$input_1 = GUICtrlCreateInput("", 30, 40, 350, 21)
$button2 = GUICtrlCreateButton("Stop Broadcasting", 150, 80, 100, 30)
GUICtrlSetState($button2, $GUI_HIDE)
$button1 = GUICtrlCreateButton("Begin Broadcasting", 150, 80, 100, 30)
$progbar = GUICtrlCreateProgress(30, 150, 350, 30, $PBS_SMOOTH)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $button1
            $data = GUICtrlRead($input_1)
            GUICtrlSetData($Label_1, $data)
            GUICtrlDelete($input_1)
            GUICtrlDelete($button1)
            GUICtrlSetState($button2, $GUI_SHOW)
            Initiate($data)
        Case $msg = $button2
            GUICtrlSetData($Label_2, "Stopping Encoder")
            Cleanup()
        Case $msg = $GUI_EVENT_CLOSE
            GUICtrlSetData($Label_2, "Stopping Encoder")
            Cleanup()
    EndSelect
WEnd

Func Initiate($var)
    elog("=============================")
    GUICtrlSetData($Label_2, "Configuring System")
    GUICtrlSetData($progbar, 0)
    $newtitle = $var
    elog(day() & time() & " User input Title: " & Chr(34) & $newtitle & Chr(34))
    
; open files for reading / re-writing
    $default = FileOpen("C:\encode\script\default.wme", 0)
    $start = FileOpen("C:\encode\script\start.wme", 2)
    If $default = -1 Then
        MsgBox(0, "Error", "Unable to open 'default.wme' file.")
        Exit
    EndIf
    If $start = -1 Then
        MsgBox(0, "Error", "Unable to open  'start.wme' file.")
        Exit
    EndIf
; map network drive for temp storing the archive file
    $drv = DriveMapAdd("*", "\\Server\ShareName$")
    elog(day() & time() & " Attempting to map Network Drive - " & $drv)
    Sleep(2000)
    If $drv = "" Then
        $drv = "C:\encode\archive"
        Select
            Case @error = 1
                $comment = "1. Undefined / Other error"
            Case @error = 2
                $comment = "2. Access to the remote share was denied"
            Case @error = 3
                $comment = "3. The device is already assigned"
            Case @error = 4
                $comment = "4. Invalid device name"
            Case @error = 5
                $comment = "5. Invalid remote share"
            Case @error = 6
                $comment = "6. Invalid password"
        EndSelect
        elog(day() & time() & " Could not map network drive. Error was: " & $comment)
    EndIf
    GUICtrlSetData($Label_2, "Creating system parameters")
    GUICtrlSetData($progbar, 25)
    
; format necessary info
    $oldtitle = "CHANGE_TITLE"
    $oldpath = "C:\CHANGE_PATH.wma"
    $name = StringLeft($newtitle, 10)
    $name = StringReplace($name, " ", "")
    $name = StringReplace($name, "/", "")
    $name = StringReplace($name, ";", "")
    $name = StringReplace($name, ",", "")
    $name = StringReplace($name, "'", "")
    $name = StringReplace($name, ":", "")
    $name = StringReplace($name, "?", "")
    $name = StringReplace($name, "~", "")
    $name = StringReplace($name, "@", "")
    $name = StringReplace($name, "!", "")
    $name = StringReplace($name, "$", "")
    $name = StringReplace($name, "*", "")
    $name = StringReplace($name, "<", "")
    $name = StringReplace($name, ">", "")
    $name = day() & $name & ".wma"
    $fullpath = $drv & "\" & $name
    elog(day() & time() & " Archive file = " & $fullpath)
    
; create a custom start.wme file
    While 1
        $line = FileReadLine($default)
        If @error = -1 Then ExitLoop; -1 means the end of the file
        If StringInStr($line, $oldtitle) Then
            $line = StringReplace($line, $oldtitle, $newtitle)
        ElseIf StringInStr($line, $oldpath) Then
            $line = StringReplace($line, $oldpath, $fullpath)
        EndIf
        FileWriteLine($start, $line)
    WEnd
    FileClose($default)
    FileClose($start)
    GUICtrlSetData($Label_2, "Starting Encoder Program")
    GUICtrlSetData($progbar, 50)
    
; run encoding program in hidden mode
    Run("C:\Program Files\Windows Media Components\Encoder\wmenc.exe c:\encode\script\start.wme", "", @SW_HIDE)
    WinActivate("Audio Encoder"); shift focus back to our app
    While 1
        Sleep(500)
        If WinExists("start - Windows Media Encoder") Then
            Sleep(2000); give it a couple seconds before proceeding
            ExitLoop
        EndIf
    WEnd
    GUICtrlSetData($Label_2, "Initiating Encoder")
    GUICtrlSetData($progbar, 75)
    
; Send keys to begin broadcasting
    ControlSend("start - Windows Media Encoder", "", "Titan:CToolBar1", "^E", 0)
    elog(day() & time() & " Started Encoding")
    
; Do some error checking to ensure the archive file is being created
    $count = 0
    While 1
        If FileExists($fullpath) Then
            elog(day() & time() & " Archive file took " & $count & " sec to create")
            ExitLoop
        Else
            Sleep(1000)
            $count = $count + 1
            If $count > 10 Then
                elog(day() & time() & " File not found within 10 seconds, killing process")
                WinKill("start - Windows Media Encoder")
                DriveMapDel($drv)
                $msg = MsgBox(0, "Error", "There has been an error, please " _
                            & "contact an administrator if this problem persists")
                Exit
            EndIf
        EndIf
    WEnd
    GUICtrlSetData($progbar, 100)
    GUICtrlSetData($Label_2, "Broadcasting Live")
EndFunc  ;==>Initiate

Func Cleanup()
    ControlSend("start - Windows Media Encoder", "", "Titan:CToolBar1", "^S", 0)
    GUICtrlSetData($progbar, 0)
    GUICtrlSetData($Label_2, "Ending Session")
    
; give the encoder enough time to stop (move progress bar while waiting)
    $movebar = 0
    While 1
        GUICtrlSetData($progbar, $movebar)
        Sleep(1000)
        $movebar = $movebar + 10
        If $movebar > 99 Then ExitLoop
    WEnd
    WinClose("start - Windows Media Encoder")
    
; wait for encoder program to close
    GUICtrlSetData($Label_2, "Closing Encoder Application")
    $movebar = 0
    While 1
        Sleep(500)
        If WinExists("start - Windows Media Encoder") Then
            GUICtrlSetData($progbar, $movebar)
            $movebar = $movebar + 10
            If $movebar > 99 Then $movebar = 0
        Else
            ExitLoop
        EndIf
    WEnd
    elog(day() & time() & " Begin FTP")
    
; FTP file to media server
    If FileExists($fullpath) Then
        GUICtrlSetData($progbar, 60)
        GUICtrlSetData($Label_2, "Sending Archive file to Media Server")
        $server = '192.168.1.1'; IP of server
        $username = 'UserName'
        $pass = "PassWord"
        $Open = _FTPOpen ('MyFTP Control')
        $Conn = _FTPConnect ($Open, $server, $username, $pass)
        $Ftpp = _FtpPutFile ($Conn, $fullpath, $name)
        $Ftpc = _FTPClose ($Open)
        elog(day() & time() & " Completed FTP of temp file")
        FileDelete($fullpath)
        elog(day() & time() & " Deleted temp file")
        GUICtrlSetData($Label_2, "Transfer Completed... Closing Application")
        GUICtrlSetData($progbar, 100)
        DriveMapDel($drv)
        Sleep(2000)
        Exit
        
    Else
        MsgBox(0, "Done", "Archived File not found.")
        elog(day() & time() & " Temp file did not exist")
        DriveMapDel($drv)
        Exit
    EndIf
    elog(day() & time() & " Closing Application Successfully")
EndFunc  ;==>Cleanup

Func elog($txt)
    $ix = FileOpen("C:\encode\script\errorlog.txt", 1)
    FileWriteLine($ix, $txt)
    If $ix = -1 Then
        MsgBox(0, "Error", "Unable to open  'error.txt' file.")
        Exit
    EndIf
    FileClose($ix)
EndFunc  ;==>elog

Func day()
    $iday = @YEAR & @MON & @MDAY
    Return $iday
EndFunc  ;==>day

Func time()
    $iTime = @HOUR & @MIN & @SEC
    Return $iTime
EndFunc  ;==>time

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0