AutoHotkey Community

It is currently May 26th, 2012, 7:20 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: AttachConsole in a loop
PostPosted: June 1st, 2009, 6:54 am 
Offline

Joined: May 31st, 2009, 6:40 am
Posts: 12
I don't know what is going on. When I comment out AttachConsole() the loop runs through everything in the folder. When AttachConsole() is enabled (for progress bar) the first file encodes just fine then the script closes (it doesn't crash) without completing the loop.

Code:
#include GetFileList.ahk
#include attachconsole.ahk
#include SimpleArray_v3_Release.ahk
;#include ParseOutput.ahk

source_dir = x:\rips   ;Source Directory
dest_dir = x:\finished_rips ;destionation directory
del = 0 ;delete source (1 yes, 2 no)
query = -f mkv -p  -e x264 -q 0.595 -a 1 -E ac3 -B 160 -R 0 -6 dpl2 -D 1 -U  -F  --markers="C:\Users\Cole\AppData\Local\Temp\%SOURCE%-chapters.csv" -x ref=4:mixed-refs=1:bframes=6:b-pyramid=1:weightb=1:deblock=-2,-1:trellis=1:analyse=all:8x8dct=1:me=umh:subq=9:psy-rd=1,1:merange=32:no-fast-pskip=1:no-dct-decimate=1:direct=auto -v
hb_path = C:\Program Files (x86)\HandBrake\handbrakeCLI.exe ; Path to Handbrake CLI
hb_priority = Low ; Sets process priority (Normal|High|Low|BelowNormal|AboveNormal)
dest_ext = mkv ; Sets your destination extension (ie m4v, mkv, avi)
file_size_error = 750 ;Min File Size Beofre Error

Gui, Color, Default, 000000
Gui, Add, Progress, w400 h20 cBlue vMyProgress
Gui, Add, Edit, x10 y25 w400 h20 cFFFFFF vRepConsole,
Gui, Show, , ConvertRips By Cole


GetFileList_ISO(source_dir)
GetFileList_VTS(source_dir)
GetNumFiles()

;Loop Through ISO Files
loopcount := 0
Loop, %count_ISO%
{
   loopcount++
   loop_source := file_list_ISO%loopcount%
   loop_source = "%loop_source%"
   loop_name := mv_name_iso%loopcount%
   loop_dest = "%dest_dir%\%loop_name%\%loop_name%.%dest_ext%"
   sleep, 3000
   WinWaitClose, %hb_path%
   encode(loop_source, loop_dest, hb_path, query)
   
}

;Loop Through VTS Files
loopcount := 0
Loop, %count_VTS%
{
   loopcount++
   loop_source := file_list_VTS%loopcount%
   loop_name := mv_name_VTS%loopcount%
   loop_dest = %dest_dir%\%loop_name%\%loop_name%.%dest_ext%
   sleep, 3000
   WinWaitClose, %hb_path%
   encode(loop_source, loop_dest, hb_path, query)
   
}


;Encodce
Encode(source_a, dest_a, hb_path, query)
{
GuiControl, , RepConsole, Encoding %source_a%
DllCall("FreeConsole")
Run, % hb_path . " -i" . source_a . " -o" . dest_a . " " . query . " -L",, Min, HB_PID
Process, Priority, %HB_PID%, %hb_priority% ; Runs handbrakeCLI and sets priority
WinWait, ahk_pid %HB_PID%
AttachConsole(HB_PID)
SetTimer, UpdateGui, 1000
Return
}

;Parse Query (takes out -v if exists)
;ParseQuery(query)
;{
;StringReplace, query, query, "-v",, 1
;Return query
;}

UpdateGui:
   
   text := GetConsoleText()
   If text = %prevText%
      Return
   prevText := text   
   ;text_pos := InStr(text, "Encoding: task")
   ;text_pos := RegExMatch(text, "Encoding: task")
   StringGetPos, text_pos, text, Encoding: task, R1

   StringTrimLeft, text, text, text_pos
   StringTrimLeft, text, text, 23         
   StringTrimRight, text, text, 52

   GuiControl,, MyProgress, % text
   ;GuiControl, , RepConsole, % text

return
   








Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2009, 12:08 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
That usually happens if you manually close the console window while the script is attached, but it shouldn't happen if the other process is simply ending. If you run the other process from cmd.exe, does the console stay open afterward?

Have you tried using a single console for all? Any console-mode processes you launch should inherit the console of the script. There shouldn't be any need to repeatedly attach and free the console. If you need to wipe the content of the console, there are other ways.
Code:
DllCall("AllocConsole")


Report this post
Top
 Profile  
Reply with quote  
 Post subject: AttachConsole()
PostPosted: June 1st, 2009, 9:12 pm 
Offline

Joined: May 31st, 2009, 6:40 am
Posts: 12
so I just need to open a console then send each execution to that console. That seems like it may work. More research -- how to send a command to a console, I don't want to do it the sloppy way.

Thanks.

I will let you know how it works out.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2009, 12:13 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
Opening a console is a simple matter of calling AllocConsole() as I demonstrated. Assuming by "send each execution to that console" you mean run a console application and have it output to that console, it is automatic - just Run the command as usual, and it will inherit the script's console (assuming it is a console application).


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Almost got it
PostPosted: June 2nd, 2009, 10:04 am 
Offline

Joined: May 31st, 2009, 6:40 am
Posts: 12
It works perfect, but only for the first encode. When I get to the second file in the loop and a new instance of handbrakecli.exe the GUI stops updating. It isn't throwing any errors though so I know I am getting close

Code:
#include GetFileList.ahk
#include attachconsole.ahk
#include ConsoleSend.ahk
;#include SimpleArray_v3_Release.ahk
;#include ParseOutput.ahk
DllCall("FreeConsole")



source_dir = x:\rips   ;Source Directory
dest_dir = x:\finished_rips ;destionation directory
del = 0 ;delete source (1 yes, 2 no)
query = -f mkv -p  -e x264 -q 0.595 -a 1 -E ac3 -B 160 -R 0 -6 dpl2 -D 1 -U  -F  --markers="C:\Users\Cole\AppData\Local\Temp\%SOURCE%-chapters.csv" -x ref=4:mixed-refs=1:bframes=6:b-pyramid=1:weightb=1:deblock=-2,-1:trellis=1:analyse=all:8x8dct=1:me=umh:subq=9:psy-rd=1,1:merange=32:no-fast-pskip=1:no-dct-decimate=1:direct=auto
hb_path = C:\Program Files (x86)\HandBrake\handbrakeCLI.exe ; Path to Handbrake CLI
hb_priority = Low ; Sets process priority (Normal|High|Low|BelowNormal|AboveNormal)
dest_ext = mkv ; Sets your destination extension (ie m4v, mkv, avi)
file_size_error = 750 ;Min File Size Beofre Error

Gui, Color, Default, 000000
Gui, Add, Progress, w400 h20 cBlue vMyProgress
Gui, Add, Edit, x10 y25 w400 h50 cFFFFFF vRepConsole,
Gui, Show, , ConvertRips By Cole
DllCall("AllocConsole")






GetFileList_ISO(source_dir)
GetFileList_VTS(source_dir)
GetNumFiles()


;Loop Through ISO Files
loopcount := 0
Loop, %count_ISO%
{
   sleep, 3000
   While encode_running = 1
   {   
      sleep 5000
      Gosub, updateGui
      Process, Exist, handbrakecli.exe
      If !ErrorLevel
         {
            encode_running := 0
            GuiControl, , RepConsole, Encoding %source_a% Finished
            sleep 3000
         }
      
   }
   loopcount++
   loop_source := file_list_ISO%loopcount%
   loop_name := mv_name_ISO%loopcount%
   loop_dest = %dest_dir%\%loop_name%\%loop_name%.%dest_ext%
   encode(loop_source, loop_dest, hb_path, query)
}

;Loop Through VTS Files
loopcount := 0
Loop, %count_VTS%
{   
   sleep, 3000
   While encode_running = 1
   {   
      sleep 5000
      Gosub, updateGui
      Process, Exist, handbrakecli.exe
      If !ErrorLevel
         {
            encode_running := 0
            GuiControl, , RepConsole, Encoding %source_a% Finished
            sleep 3000
         }
      
   }   
   loopcount++
   loop_source := file_list_VTS%loopcount%
   loop_name := mv_name_VTS%loopcount%
   loop_dest = %dest_dir%\%loop_name%\%loop_name%.%dest_ext%
   encode_running :=0
   encode(loop_source, loop_dest, hb_path, query)
}


;Encode

Encode(source_a, dest_a, hb_path, query)  ;Start hbcli and get text from console
{
global

DllCall("FreeConsole")

encode_running := 1
GuiControl, , RepConsole, Encoding %source_a%

Run, % hb_path . " -i """ . source_a . """ -o """ . dest_a . """ " . query . " -L",, min, HB_PID
Process, Priority, %HB_PID%, %hb_priority% ; Runs handbrakeCLI and sets priority
WinWait, ahk_pid %HB_PID%
AttachConsole(HB_PID)

Return
}

;Parse Query (takes out -v if exists)  ;for future use (do before releasing)
;ParseQuery(query)
;{
;StringReplace, query, query, "-v",, 1
;Return query
;}

UpdateGui:  ;Update GUI and parse text from console

   text := GetConsoleText()
   If text = %prevText%
      Return
   prevText := text   
   ;text_pos := InStr(text, "Encoding: task")
   ;text_pos := RegExMatch(text, "Encoding: task")
   
   
   StringGetPos, p_comp_L, text, Encoding: task, R1
   p_comp_L := p_comp_L+28
   StringMid, p_comp, text, %p_comp_L%, 5 , L
   
   StringGetPos, fps_L, text, `% (, R1
   fps_L := fps_L+7
   StringMid, fps, text, %fps_L%, 4 , L
   
   StringGetPos, afps_L, text, avg, R1
   afps_L := afps_L+8
   StringMid, afps, text, %afps_L%, 4 , L
   
   StringGetPos, ETA_L, text, ETA, R1
   ETA_L := ETA_L+8
   StringMid, ETA, text, %ETA_L%, 9 , L


   GuiControl,, MyProgress, % p_comp
   GuiControl, , RepConsole, FPS:%fps% AVG FPS:%afps% ETA:%ETA%


return


Thanks Lexikos, without your help I would have stopped improving this project. And please excuse my messy code,


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2009, 10:36 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
Your script still calls FreeConsole() before launching the process and AttachConsole() after, which defeats the point of calling AllocConsole(). Try removing all of the FreeConsole() and AttachConsole() calls.

To reiterate: calling AllocConsole() creates a console and associates the calling process with it. Console applications launched by the script are associated with the script's console automatically; no further action should be necessary.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Nevermind!
PostPosted: June 2nd, 2009, 10:40 am 
Offline

Joined: May 31st, 2009, 6:40 am
Posts: 12
I got it to work. I had to reduce the screen buffer height from the console properties menu.

I think the way HandbrakeCLI updates it's info was weirding out your script and this somehow fixed it.

Now off to make that buffer size change automatic, make my GUI, and learn how to use simplearrays for storing my filenames and associated data.

I'm sure I'll be back here often, maybe I should have just used C++


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2009, 10:49 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
Quote:
Now off to make that buffer size change automatic,

I suppose you could use SetConsoleScreenBufferSize and/or SetConsoleWindowInfo.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2009, 9:09 am 
Offline

Joined: May 31st, 2009, 6:40 am
Posts: 12
Everything is working great now!

Have you noticed that your script won't retrieve the buffer when the console is hidden. I am using Win7 RC1 so that may break it if it has worked before. If you know of a work around for that, it would be great, otherwise no biggie.

On another note I plan on releasing this as open source with a donate button for wounded warriors project, or something similar. Let me know if you have any problems with me using your AttachConsole() function. Your name is credited in the source.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 3rd, 2009, 10:33 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
colek42 wrote:
Have you noticed that your script won't retrieve the buffer when the console is hidden.
Do you mean this one? WinGet only works on hidden windows if DetectHiddenWindows is On. Actually, I tested just now with cmd.exe (64-bit), and the PID returned by WinGet was somehow incorrect (it didn't match what was in Task Manager), so AttachConsole failed and the script didn't work whether or not the window was hidden. Using the following to retrieve the PID, it worked either way:
Code:
Process, Exist, cmd.exe
pid := ErrorLevel
I'm on Windows 7 RC 64-bit, but I was on Vista 32-bit when I wrote that script.
Quote:
Let me know if you have any problems with me using your AttachConsole() function.
I don't recall writing an AttachConsole() function, but I did write code which n-l-i-d (presumably daonlyfreez) wrapped into a function.

Unless otherwise noted, any script I post on these forums may be used (etc.) as outlined in this thread.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 4th, 2009, 7:23 am 
Offline

Joined: May 31st, 2009, 6:40 am
Posts: 12
ok im thinking for the SetConsoleScreenBufferSize I have to do something like this:

Code:
 hConOut:=DllCall("CreateFile","str","CONOUT$","uint",0xC0000000
                      ,"uint",7,"uint",0,"uint",3,"uint",0,"uint",0)
  if hConOut = -1 ; INVALID_HANDLE_VALUE
  {
      MsgBox CreateFile failed - error %A_LastError%.
      ExitApp
  }

if !DllCall("SetConsoleScreenBufferSize","uint",hConOut,","x80y20",)
  {
      MsgBox SetConsoleScreenBufferSize failed - error %A_LastError%.
      ExitApp
  }


AHK does not let me create a COORD variable, I have no clue how to do this without one.

Right now I have AHK go through the menus to set the buffer size...very messy.

Again thanks for your help, it has been indispensable


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 4th, 2009, 8:15 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
COORD is a 32-bit (4 byte) structure, and therefore can be passed by value as UInt:
Code:
DllCall("SetConsoleScreenBufferSize","uint",hConOut,"uint", w | h<<16 )

Since you can neither shrink the buffer below the current window size nor expand the window beyond the current buffer size, the order you call SetConsoleWindowInfo and SetConsoleScreenBufferSize is important. For example, this will work, but only if the default/previous buffer size was >= 80,20:
Code:
w := 80, h := 20
DllCall("SetConsoleWindowInfo","uint",hConOut,"int",1,"int64*", w-1 <<32 | h-1 <<48 )
DllCall("SetConsoleScreenBufferSize","uint",hConOut,"uint", w | h <<16 )
SetConsoleScreenBufferInfoEx, which is available on Windows Vista and later, presumably solves this issue by allowing both to be resized simultaneously.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Alpha Bravo, bobbysoon, BrandonHotkey, Cephei1, joetazz, krajan, Miguel, over21, rbrtryn, SKAN and 69 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group