Jump to content

Recommended Posts

Posted

Hi. I’m currently working on a script where all given audio files are converted to wav in 48kHz and 24bit with sox. It works like charme. All I need now is a progress bar. I first tried to calculate the progress with current file size and estimated size. Therefor I used the read out bitrate and the needed factor to the new bitrate. But there is always a little gap with the header etc. and I have to ask current file size a lot of times. 

Now I found the progress bar of sox. If I run sox in cmd.exe with parameter -S the progress status is a static line which permanent shows the actual percentage of the work (and mor). It’s very fine graded, it has a high repetition rate. I read that I had to catch this progress with stderr. If I catch the updates are very lame. Stderr updates only every 6 seconds. It collects all the fine graded percentages but puts them out as a long list of rising percentages. So I only can use the last one for setting the progress bar (all others are lower and gone of cause). You can the list of them I can use. Sox ran 1m30s but only gave me 15 lists of fine graded rising percentages.

0.00
6.53
12.9
18.7
24.7
31.8
38.7
45.2
51.2
57.3
63.6
70.2
77.9
85.8
93.9

01m 29s

Currently I can’t give you a running example. And if I could it would have 5 FileInstall() needed.

Is it normal behavior of stderr catching a lot of dates and spitting them out collected in great blocks?

Regards, Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Posted (edited)

Ha, forgot. Had my script on pastebin, too. It’s a bit work in progress:

#include <AutoItConstants.au3>
#include <Array.au3> ; Debug only

Local $sCurTrack, $sCurTrackMinusExt, $sCurTrackNewExt, $sCurTrackName
Local $sOutput, $sOutError, $iPID ; Auslesen STDOUT + STDERR
Local $aOutput, $aBitRate, $iBitRate, $iFaktorFileSize
Local $iOldSize, $iShouldSize, $iTempSize, $iProzent, $iProzentTemp = 0
Local $aProzent

FileInstall("H:\_Conrad lokal\Downloads\AutoIt3\_COX\sox\zlib1.dll", @TempDir & "\", 1)
FileInstall("H:\_Conrad lokal\Downloads\AutoIt3\_COX\sox\pthreadgc2.dll", @TempDir & "\", 1)
FileInstall("H:\_Conrad lokal\Downloads\AutoIt3\_COX\sox\libgomp-1.dll", @TempDir & "\", 1)
FileInstall("H:\_Conrad lokal\Downloads\AutoIt3\_COX\sox\libmp3lame.dll", @TempDir & "\", 1)
FileInstall("H:\_Conrad lokal\Downloads\AutoIt3\_COX\sox\libmad.dll", @TempDir & "\", 1)
FileInstall("H:\_Conrad lokal\Downloads\AutoIt3\_COX\sox\sox.exe", @TempDir & "\", 1)

Opt("GuiCoordMode", 0) ;relative position to the start of the last control with "-1"
Opt("GuiOnEventMode", 1)
Local $iWidthLabel = 400
Local $hGUI = GUICreate(StringTrimRight(@ScriptName, 4),$iWidthLabel + 40,950) ; 150 is correct height later
GUISetFont(10)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Beenden")
Local $hLabelCounter = GUICtrlCreateLabel("Dateien werden gewandelt: ", 20, 20, $iWidthLabel)
Local $hProgress = GUICtrlCreateProgress(-1, 25, $iWidthLabel)
Local $sLabelFile = GUICtrlCreateLabel(".", -1, 32, $iWidthLabel, 880) ; 80 is the correct height later
GUICtrlSetFont(-1,9)
GUISetState()
Local $hTimerStart = TimerInit()

For $i = 1 To $CmdLine[0]
    $sCurTrack = $CmdLine[$i]
    ConsoleWrite("ShortName: " & $sCurTrack & @CRLF)
    $sCurTrackMinusExt = StringTrimRight($sCurTrack, 4)
    $sCurTrackNewExt = '_48kHz_24bit.wav'

    $sOutput = "" ; clear
    $sOutError = "" ; clear
    $iPID = Run(@TempDir & "\sox.exe --i " & _quotePath($sCurTrack), "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) ; get all Metadata
    While ProcessExists($iPID)
        $sOutput &= StdoutRead($iPID)
        $sOutError &= StderrRead($iPID)
    WEnd

    If $sOutput = "" Then ; no Metadata
        $sOutError = StringRegExpReplace($sOutError, ".*\\sox.exe", "")
        MsgBox($MB_SYSTEMMODAL + $MB_ICONWARNING, @ScriptName, $sCurTrack & ":" & @CRLF & @CRLF & $sOutError)
    Else
        GUICtrlSetData($sLabelFile, _WordWrapTextForLabel($sCurTrack, 59))
        GUICtrlSetData($hLabelCounter, "Dateien werden gewandelt: " & $i & "/" & $CmdLine[0])
        $aOutput = StringSplit($sOutput, @CRLF)
        $aBitRate = StringRegExp($aOutput[15], "[\d.]{1,5}", 1) ; Bitrate
        $iBitRate = $aBitRate[0]
        If StringInStr($iBitRate,".") Then ; if in 2.31M instead 192k
            $iBitRate *= 1024 ; mbit to kbit
        EndIf
        ConsoleWrite("BitRate: " & $iBitRate & @CRLF)
        $iFaktorFileSize = 2365.44 / $iBitRate ; 2365.44 = Bitrate of 24bit and 48kHz
        ConsoleWrite("FaktorFileSize: " & $iFaktorFileSize & @CRLF)

############### current solution of a progress bar ##################
    $iPID = Run(@TempDir & "\sox.exe " & _quotePath($sCurTrack) & " -b 24 -r 48k " & _quotePath($sCurTrackMinusExt & $sCurTrackNewExt) & " -S", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) ; run converter
    Local $hTimerStart = TimerInit()
    While ProcessExists($iPID)
;~      $sOutput &= StdoutRead($iPID) ; there are no progressing infos
        $sOutError = StderrRead($iPID)

;~  If $sOutError <> "" Then ; no Metadata
        $aProzent = StringRegExp($sOutError, "(?i)In:([\d.]*)%", 1)
        If Not @error Then
            $iProzent = $aProzent[UBound($aProzent) - 1]
            ConsoleWrite($iProzent & @CRLF)
            GUICtrlSetData($sLabelFile, $iProzent)
            GUICtrlSetData($hProgress,$iProzent)
        EndIf
;~  EndIf

    WEnd
    ConsoleWrite($sOutError & @CRLF)
    ConsoleWrite(_Zeit(TimerDiff($hTimerStart), True) & @CRLF) ; converting took XXm XXs

; ############ older solution of a progress bar ###############
;~      $iPID = ShellExecute(@TempDir & "\sox.exe", _quotePath($sCurTrack) & " −b 24 -r 48k " & _quotePath($sCurTrackMinusExt & $sCurTrackNewExt),"","", @SW_HIDE) ; run converter

;~      $iOldSize = FileGetSize($sCurTrack)
;~      ConsoleWrite("OldSize: " & $iOldSize & @CRLF)
;~      $iShouldSize = Round($iOldSize * $iFaktorFileSize)
;~      ConsoleWrite("ShouldSize: " & $iShouldSize & @CRLF)
;~      $sCurTrackName = StringTrimLeft($sCurTrack, StringInStr($sCurTrack, "\", 0, -1))
;~      While ProcessExists($iPID)
;~          $iTempSize = FileGetSize($sCurTrackMinusExt & $sCurTrackNewExt)
;~          $iProzent = Round(100 * $iTempSize / $iShouldSize)
;~          If $iProzent <> $iProzentTemp Then
;~              ConsoleWrite($iProzent & "% ")
;~              $iProzentTemp = $iProzent
;~          EndIf
;~          GUICtrlSetData($hProgress,$iProzent)
;~      WEnd
;~      ConsoleWrite(@CRLF)
; ####################################################
        Sleep(100)
    EndIf
    ConsoleWrite(@CRLF)
Next
Exit

Func _quotePath($sPath) ; versieht Pfade mit Anführungszeichen " - nötig für Run sowie die Parameter in ShellExecute
    $sPath = '"' & $sPath & '"'
    Return $sPath
EndFunc

Func _WordWrapTextForLabel($sText, $iCounter)
    Local $sResult
    Local $iTotalChar = StringLen($sText)
    For $i = 1 To Ceiling($iTotalChar / $iCounter)
        $sResult &= StringLeft($sText, $iCounter) & " "
        $sText = StringTrimLeft($sText, $iCounter)
    Next
    $sResult = StringTrimRight($sResult, 1) ; cut last " "
    Return $sResult
EndFunc

Func _Zeit($iMs, $bComfortView = False) ; from ms to a format: "12h 36m 56s 13f" (with special space between - ChrW(8239))
    Local $sReturn
    $iMs = Int($iMs)
    Local $iFrames, $iMSec, $iSec, $iMin, $iHour, $sSign
    If $iMs < 0 Then
        $iMs = Abs($iMs)
        $sSign = '-'
    EndIf
    $iMSec = StringRight($iMs, 3)
    $iFrames = $iMSec / 40
    $iSec = $iMs / 1000
    $iMin = $iSec / 60
    $iHour = $iMin / 60
    $iMin -= Int($iHour) * 60
    $iSec -= Int($iMin) * 60
    If $bComfortView Then ; no hours if not present and no frames
        If Not Int($iHour) = 0 Then $sReturn &= StringRight('0' & Int($iHour), 2) & 'h' & ChrW(8239)
        $sReturn &= StringRight('0' & Int($iMin), 2) & 'm' & ChrW(8239) & StringRight('0' & Int($iSec), 2) & 's'
    Else
        $sReturn = $sSign & StringRight('0' & Int($iHour), 2) & 'h' & ChrW(8239) & StringRight('0' & Int($iMin), 2) & 'm' & ChrW(8239) & StringRight('0' & Int($iSec), 2) & 's' & ChrW(8239) & StringRight('0' & Int($iFrames), 2) & 'f'
    EndIf
    Return $sReturn
EndFunc

Func _Beenden()
    Exit
EndFunc

If someone will try it of cause you need sox. Tomorrow I could zip that 6 files and upload.

Conrad

Edited by Simpel
6 instead 5 files needed, typo
SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

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
×
×
  • Create New...