Jump to content

Recommended Posts

I want to make a program for any video to av1 video converter GUI using FFMpeg. Of course for myself.

#include <GUIConstants.au3>
#include <Constants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>

$hGUI = GUICreate("AutoIt CMD", 600, 420, -1, -1)
$input = GUICtrlCreateInput("", 75, 10, 435, 25, 2048)
GUICtrlSetFont(-1, 12)
$vbitrate = GUICtrlCreateInput("", 90, 50, 50, 20)
$abitrate = GUICtrlCreateInput("", 220, 50, 50, 20)
$Final = GUICtrlCreateInput("", 10, 80, 500, 50, $ES_WANTRETURN + $ES_MULTILINE)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button1 = GUICtrlCreateButton("Dosya", 10, 10, 60, 25)
GUICtrlSetFont(-1, 12)
$Button2 = GUICtrlCreateButton("İPTAL ET", 360, 50, 70, 20)
$Button3 = GUICtrlCreateButton("SIFIRLA", 280, 50, 70, 20)
GUICtrlSetState($Button2, $GUI_DISABLE)
$vlabel = GUICtrlCreateLabel("Video Bitrate:", 20, 53, 70, 20)
$alabel = GUICtrlCreateLabel("Ses Bitrate:", 150, 53, 70, 20)
$cExecute = GUICtrlCreateButton("ÇEVİR", 520, 5, 70, 125)
$cOutputBox = GUICtrlCreateEdit("", 10, 140, 580, 260, 2048 + 4 + 0x00200000)
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $Button1
        $file = FileOpenDialog("Dönüştürülecek dosya seçin",@DesktopDir & "\","All Files (*.*)")
        GUICtrlSetdata($input, $file)
        $folder = stringleft($file , StringInStr($file , "\" , 0, -1))
        $folder1 = FileGetShortName($file)
        $folder2 = stringleft($file , StringInStr($file , "\" , 0, -1))
        $folder3 = FileGetShortName($folder2)

        Case $Button2
            ProcessClose("ffmpeg.exe")

        Case $Button3
            GUICtrlSetState($vbitrate, $GUI_ENABLE)
            GUICtrlSetState($abitrate, $GUI_ENABLE)
            GUICtrlSetState($Button1, $GUI_ENABLE)
            GUICtrlSetState($Button2, $GUI_DISABLE)
            GUICtrlSetData($vbitrate, "")
            GUICtrlSetData($abitrate, "")
            GUICtrlSetData($input, "")
            GUICtrlSetData($cOutputBox, "")
            GUICtrlSetData($Final, "")
        Case $cExecute
            GUICtrlSetState($Button2, $GUI_ENABLE)
            GUICtrlSetState($Button1, $GUI_DISABLE)
            GUICtrlSetState($vbitrate, $GUI_DISABLE)
            GUICtrlSetState($abitrate, $GUI_DISABLE)
            $vb = GuiCtrlRead($vbitrate)
            $ab = GuiCtrlRead($abitrate)
            GuiCtrlSetData($Final, "ffmpeg -v 16 -stats -i " & $folder1 & " -c:a libopus -ac 2 -b:a "& $ab & "k -c:v libsvtav1 -preset 1 -svtav1-params tier=2:rc=1:tbr=" & $vb & " -y " & $folder3 & "av1.mp4")
            $sCommand = GUICtrlRead($Final)
            If StringStripWS($sCommand, 8) <> "" Then
                GUICtrlSetState($cExecute, 128)
                GUICtrlSetData($cExecute, " Çevriliyor... ")
                _ExecuteCommandStream($sCommand)
                GUICtrlSetData($cOutputBox, _ExecuteCommand($sCommand))
                GUICtrlSetData($cExecute, "ÇEVİR")
                GUICtrlSetState($cExecute, 64)
            EndIf
    EndSwitch
WEnd

Func _ExecuteCommandStream($sCommand)
    Local $Finalinfo = Run('"' & @ComSpec & '" /c ' & $sCommand, @ScriptDir, @SW_HIDE, 8)
    Local $sOutput, $sOutputError
    While 1
        GUICtrlSetData($cOutputBox, $sOutput & @CRLF)
        $sOutput &= StdoutRead($Finalinfo)

        If @error Then ExitLoop
        Sleep(50)
    WEnd
    GUICtrlSetData($cOutputBox, $sOutput & @CRLF)
EndFunc

Func _ExecuteCommand($sCommand)
    Local $Finalinfo = Run('"' & @ComSpec & '" /c ' & $sCommand, @ScriptDir, @SW_HIDE, 4 + 2 + 1)
    Local $sOutput, $sOutputError
    While 1
        $sOutput &= StdoutRead($Finalinfo)
        If @error Then ExitLoop
    WEnd
    While 1
        $sOutputError &= StderrRead($Finalinfo)
        If @error Then ExitLoop
            GUICtrlSetState($vbitrate, $GUI_ENABLE)
            GUICtrlSetState($abitrate, $GUI_ENABLE)
            GUICtrlSetState($Button1, $GUI_ENABLE)
            GUICtrlSetState($Button2, $GUI_DISABLE)
            GUICtrlSetData($vbitrate, "")
            GUICtrlSetData($abitrate, "")
            GUICtrlSetData($input, "")
            GUICtrlSetData($Final, "")
    WEnd
    If $sOutput <> '' Then
        Return $sOutput
    ElseIf $sOutputError <> '' Then
        Return SetError(0, 1, $sOutputError)
    Else
        Return SetError(0, 2, "")
    EndIf
EndFunc

Original codes taken from:

 

Here are my questions:

- Let's say we selected a video file, enter video and audio bitrate (in Turkish audio means "ses"), but we decided or have to cancel it. So I added a cancel button (in Turkish "İptal") for kill or terminate or whatever for ffmpeg. Cause if I can terminate ffmpeg.exe, process will abort "immediately". Normally, ffmpeg conversions can abort with "q" key but, this is not windows cmd window(and I don't know how to send "q" to this GUI) and "q" key can't abort "immediately", it waits some time to stop process. As you can see in video, I can't terminate "ffmpeg.exe" with ProcessClose. When I open Task Manager and right-click - Kill process, It can't be terminated and starts new process: conhost.exe

When I right-click and terminate second time, it can be terminate.

Why ProcessClose not working simply? (killtask, winapi terminate etc. don't work)

Not: Normal way of ffmpeg conversion in windows's cmd.exe, you can terminate ffmpeg.exe from Task Manager without problem.

- As you can see, I can't drag slider down. It keeps coming up. How can I drag it or actually how can writings go up without having to pull slider up?

- Regarding the above question, in normal cmd window, ffmpeg converting process can be seen in one line(with my options). But in this gui, it continously adds line(you can see in video). Anybody know why?

 

 

Link to post
Share on other sites
11 hours ago, Atakanbasturk said:

I can't terminate "ffmpeg.exe" with ProcessClose

While ffmpeg is running you are stuck in the _ExecuteCommandStream While loop
Here is a way to make the stop button work (as mentioned in the wiki here)
Pseudocode :

Global $stop = 0
; ...
$Button2 = GUICtrlCreateButton("IPTAL ET", 360, 50, 70, 20)
; ...
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 #forceref $hWnd, $Msg, $lParam
   If BitAND($wParam, 0xFFFF) = $Button2 Then $stop = 1
  Return 'GUI_RUNDEFMSG'
EndFunc  

Func _ExecuteCommandStream($sCommand)
    Local $Finalinfo = Run('"' & @ComSpec & '" /c ' & $sCommand, @ScriptDir, @SW_HIDE, 8)
    While 1
        If $stop = 1 Then Exitloop
        ; ...
    WEnd
    ProcessClose("ffmpeg.exe")
EndFunc

 

Link to post
Share on other sites
Posted (edited)
#include <GUIConstants.au3>
#include <Constants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>

Global $stop = 0
$hGUI = GUICreate("AutoIt CMD", 600, 420, -1, -1)
$input = GUICtrlCreateInput("", 75, 10, 435, 25, 2048)
GUICtrlSetFont(-1, 12)
$vbitrate = GUICtrlCreateInput("", 90, 50, 50, 20)
$abitrate = GUICtrlCreateInput("", 220, 50, 50, 20)
$Final = GUICtrlCreateInput("", 10, 80, 500, 50, $ES_WANTRETURN + $ES_MULTILINE)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button1 = GUICtrlCreateButton("Dosya", 10, 10, 60, 25)
GUICtrlSetFont(-1, 12)
$Button2 = GUICtrlCreateButton("IPTAL ET", 360, 50, 70, 20)
GUICtrlSetState($Button2, $GUI_DISABLE)
$Button3 = GUICtrlCreateButton("SIFIRLA", 280, 50, 70, 20)
$vlabel = GUICtrlCreateLabel("Video Bitrate:", 20, 53, 70, 20)
$alabel = GUICtrlCreateLabel("Ses Bitrate:", 150, 53, 70, 20)
$cExecute = GUICtrlCreateButton("ÇEVIR", 520, 5, 70, 125)
$cOutputBox = GUICtrlCreateEdit("", 10, 140, 580, 260, 2048 + 4 + 0x00200000)
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $Button1
        $file = FileOpenDialog("Dönüstürülecek dosya seçin",@DesktopDir & "\","All Files (*.*)")
        GUICtrlSetdata($input, $file)
        $folder = stringleft($file , StringInStr($file , "\" , 0, -1))
        $folder1 = FileGetShortName($file)
        $folder2 = stringleft($file , StringInStr($file , "\" , 0, -1))
        $folder3 = FileGetShortName($folder2)

        Case $Button3
            GUICtrlSetState($vbitrate, $GUI_ENABLE)
            GUICtrlSetState($abitrate, $GUI_ENABLE)
            GUICtrlSetState($Button1, $GUI_ENABLE)
            GUICtrlSetState($Button2, $GUI_DISABLE)
            GUICtrlSetData($vbitrate, "")
            GUICtrlSetData($abitrate, "")
            GUICtrlSetData($input, "")
            GUICtrlSetData($cOutputBox, "")
            GUICtrlSetData($Final, "")
        Case $cExecute
            GUICtrlSetState($Button2, $GUI_ENABLE)
            GUICtrlSetState($Button1, $GUI_DISABLE)
            GUICtrlSetState($Button3, $GUI_DISABLE)
            GUICtrlSetState($vbitrate, $GUI_DISABLE)
            GUICtrlSetState($abitrate, $GUI_DISABLE)
            $vb = GuiCtrlRead($vbitrate)
            $ab = GuiCtrlRead($abitrate)
            GuiCtrlSetData($Final, "ffmpeg -v 16 -stats -i " & $folder1 & " -c:a libopus -ac 2 -b:a "& $ab & "k -c:v libsvtav1 -preset 1 -svtav1-params tier=2:rc=1:tbr=" & $vb & " -y " & $folder3 & "av1.mp4")
            $sCommand = GUICtrlRead($Final)

                GUICtrlSetState($cExecute, 128)
                GUICtrlSetData($cExecute, " Çevriliyor... ")
                _ExecuteCommandStream($sCommand)
                GUICtrlSetData($cExecute, "ÇEVIR")
                GUICtrlSetState($cExecute, 64)
                GUICtrlSetState($Button3, $GUI_ENABLE)

    EndSwitch
WEnd

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 #forceref $hWnd, $Msg, $lParam
   If BitAND($wParam, 0xFFFF) = $Button2 Then $stop = 1
  Return 'GUI_RUNDEFMSG'
EndFunc

Func _ExecuteCommandStream($sCommand)
    Local $Finalinfo = Run('"' & @ComSpec & '" /c ' & $sCommand, @ScriptDir, @SW_HIDE, 8)
    Local $sOutput, $sOutputError
    While 1
        If $stop = 1 Then Exitloop
        GUICtrlSetData($cOutputBox, $sOutput & @CRLF)
        $sOutput &= StdoutRead($Finalinfo)
    WEnd
    ProcessClose("ffmpeg.exe")

EndFunc

Func _ExecuteCommand($sCommand)
    Local $Finalinfo = Run('"' & @ComSpec & '" /c ' & $sCommand, @ScriptDir, @SW_HIDE, 4 + 2 + 1)
    Local $sOutput, $sOutputError
    While 1
        $sOutput &= StdoutRead($Finalinfo)
        If @error Then ExitLoop
    WEnd
    While 1
        $sOutputError &= StderrRead($Finalinfo)
        If @error Then ExitLoop
    WEnd
    If $sOutput <> '' Then
        Return $sOutput
    ElseIf $sOutputError <> '' Then
        Return SetError(0, 1, $sOutputError)
    Else
        Return SetError(0, 2, "")
    EndIf
EndFunc

 

Thanks for the help. I added your code to my code but I think I'm missing something. Because when I press IPTAL ET button, ffmpeg conversion aborting but ffmpeg.exe not completely closing. So script still runs and I can't use GUI again or close it. I must completely close ffmpeg.exe or I can't do anything form GUI. I want to completely close ffmpeg.exe and stop script from running. (Not closing GUI) Still I have to close from Task Manager. (Probably first it close first time and still have to second time)

 

Also OutputBox flashes continously. I added video.

https://streamable.com/e/ugkhmn

 

Edit: When I remove "GUICtrlSetData($cOutputBox, _ExecuteCommand($sCommand))" line from Case $cExecute, your code works perfectly.(I mean loop stops; that's what I want from Terminating ffmpeg). But, when I press ÇEVİR ($cExecute) button, outputbox flashes when ffmpeg works and when ffmpeg finishes job sucsessfully, loop still continue so I can't do anything from GUI.

Edit 2: I can sucsessfully terminate ffmpeg and stop loop as I mentioned above, but I can't start it again sucsessfully without closing and reopening script. (Long story short, I need reset GUI button. I tried putting GUI inside Func, but I messed up whole code. My SIFIRLA (in English: Reset) button, clear all inputs and reset buttons but when I start ÇEVİR without closing and opening GUI, loop doesn't start as it's supposed to be.It starts, ffmpeg converts file but outputbox is empty. So I can't see any progress. And cancel(İPTAL ET) button doesn't work)

https://streamable.com/e/o6scrk

 

If you want to try and see:

FFmpeg: https://www.mediafire.com/file/8xkvn3l29427aor/ffmpeg.rar/file (I compressed it with winrar)

AVI sample: https://www.engr.colostate.edu/me/facil/dynamics/files/drop.avi

Edited by Atakanbasturk
Link to post
Share on other sites
4 hours ago, Atakanbasturk said:

I can't start it again sucsessfully without closing and reopening script

As your script opens ffmpeg twice (_ExecuteCommandStream + _ExecuteCommand), to get a full and clean stop you have to close ffmpeg twice too :P
When done, don't forget to reset the $stop global variable to 0 if you want to use ffmpeg again

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...