Jump to content
dynamitemedia

stringregexp question/help

Recommended Posts

dynamitemedia

Ok so im trying to use ffmpeg to get the Dshow devices available 

Here is what i have so far

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

Global $sFolder = @ScriptDir & "\www\videos"
Global $FFmpegDir =  @ScriptDir & "\www\bin\"

$sCommands = "FFmpeg -list_devices true -f dshow -i dummy"
$iPID = Run("cmd /c " & $sCommands, $FFmpegDir , @SW_Hide , $stderr_child + $stdout_child)

$sOutput = ""
 While 1
        $sOutput &= StderrRead($iPID)

        If @error Then ; Exit the loop if the process closes or StderrRead returns an error.
            ExitLoop
        EndIf
     WEnd

;ConsoleWrite($sOutput  & @CRLF)

Local $iPosition = StringInStr($sOutput, "[dshow")
Local $sString = StringTrimLeft($sOutput, $iPosition -1)
Local $iPosition2 = StringInStr($sString, "dummy: Immediate exit requested")
Local $iLength = StringLen($sString)
Local $CutRight = $iLength - $iPosition2
Local $dStrings = StringTrimRight($sString, $CutRight + 1)
ConsoleWrite($dStrings  & @CRLF)


this is what i get back:

[dshow @ 02b85e40] DirectShow video devices
[dshow @ 02b85e40]  "CyberLink Webcam Splitter"
[dshow @ 02b85e40]  "USB Video Device"
[dshow @ 02b85e40]  "IP Camera [JPEG/MJPEG]"
[dshow @ 02b85e40]  "DroidCam"
[dshow @ 02b85e40] DirectShow audio devices
[dshow @ 02b85e40]  "USB Audio Device"
[dshow @ 02b85e40]  "Realtek HD Audio Input"


all i really want is the devices split up can come back as an array so i can put into a combo box for each of the below

1.) DirectShow video devices
2.)  DirectShow audio devices

each machine i am assuming will have different  than this "[dshow @ 02b85e40]"  so not a 100 % sure i could/should just do another "StringTrimLeft" just in case its more letters/numbers.

heck i prob didn't need what i had done as it is... sure there is an easier way

Thank you for your help

Share this post


Link to post
Share on other sites
jguinch

This ?

#Include <Array.au3>

$dStrings = '[dshow @ 02b85e40] DirectShow video devices' & @CRLF & _
           '[dshow @ 02b85e40]  "CyberLink Webcam Splitter"' & @CRLF & _
           '[dshow @ 02b85e40]  "USB Video Device"' & @CRLF & _
           '[dshow @ 02b85e40]  "IP Camera [JPEG/MJPEG]"' & @CRLF & _
           '[dshow @ 02b85e40]  "DroidCam"' & @CRLF & _
           '[dshow @ 02b85e40] DirectShow audio devices' & @CRLF & _
           '[dshow @ 02b85e40]  "USB Audio Device"' & @CRLF & _
           '[dshow @ 02b85e40]  "Realtek HD Audio Input"'

$sVideo = StringRegExpReplace($dStrings, '(?s).*\[[^\]]+\]\h+DirectShow video devices\R((?:\[[^\]]+\]\h+"[^"]+"(?:\R|$))+).*', "$1")
$aVideoDevices = StringRegExp($sVideo, '"([^"]+)"', 3)
_ArrayDisplay($aVideoDevices, "Video devices")


$sAudio = StringRegExpReplace($dStrings, '(?s).*\[[^\]]+\]\h+DirectShow audio devices\R((?:\[[^\]]+\]\h+"[^"]+"(?:\R|$))+).*', "$1")
$aAudioDevices = StringRegExp($sAudio, '"([^"]+)"', 3)
_ArrayDisplay($aAudioDevices, "Audio devices")

 

Share this post


Link to post
Share on other sites
mikell

'nother one ...  :)

#Include <Array.au3>

$dStrings = '[dshow @ 02b85e40] DirectShow video devices' & @CRLF & _
           '[dshow @ 02b85e40]  "CyberLink Webcam Splitter"' & @CRLF & _
           '[dshow @ 02b85e40]  "USB Video Device"' & @CRLF & _
           '[dshow @ 02b85e40]  "IP Camera [JPEG/MJPEG]"' & @CRLF & _
           '[dshow @ 02b85e40]  "DroidCam"' & @CRLF & _
           '[dshow @ 02b85e40] DirectShow audio devices' & @CRLF & _
           '[dshow @ 02b85e40]  "USB Audio Device"' & @CRLF & _
           '[dshow @ 02b85e40]  "Realtek HD Audio Input"'

$nV = "DirectShow video devices"
$nA = "DirectShow audio devices"
$dev = StringRegExp($dStrings, '(?s)(' & $nV & '.*)(' & $nA & '.*)', 3)

$video = StringRegExp($dev[0], '"([^"]+)"' , 3)
_ArrayDisplay($video, $nV)

$audio = StringRegExp($dev[1], '"([^"]+)"' , 3)
_ArrayDisplay($audio, $nA)

 

Edited by mikell

Share this post


Link to post
Share on other sites
dynamitemedia

thanks guys i like both of these.  is there any advantage of using one or the other?

 

Share this post


Link to post
Share on other sites
dynamitemedia

this is what i did, is there a way to make the for loops into one? to clean it up?
 

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#Include <Array.au3>

Global $FFmpegDir =  @ScriptDir & "\www\bin\"

$sCommands = "FFmpeg -list_devices true -f dshow -i dummy"
$iPID = Run("cmd /c " & $sCommands, $FFmpegDir , @SW_Hide , $stderr_child + $stdout_child)

$sOutput = ""
 While 1
        $sOutput &= StderrRead($iPID)

        If @error Then ; Exit the loop if the process closes or StderrRead returns an error.
            ExitLoop
        EndIf
     WEnd


$sVideo = StringRegExpReplace($sOutput, '(?s).*\[[^\]]+\]\h+DirectShow video devices\R((?:\[[^\]]+\]\h+"[^"]+"(?:\R|$))+).*', "$1")
$aVideoDevices = StringRegExp($sVideo, '"([^"]+)"', 3)

$sAudio = StringRegExpReplace($sOutput, '(?s).*\[[^\]]+\]\h+DirectShow audio devices\R((?:\[[^\]]+\]\h+"[^"]+"(?:\R|$))+).*', "$1")
$aAudioDevices = StringRegExp($sAudio, '"([^"]+)"', 3)


Local $vDshow = ""
For $vid In $aVideoDevices
    $vDshow = $vDshow & $vid & @CRLF
 Next
MsgBox(0, "DirectShow video devices", $vDshow)


Local $aDshow = ""
For $aud In $aAudioDevices
    $aDshow = $aDshow & $aud & @CRLF
 Next
MsgBox(0, "DirectShow audio devices", $aDshow)

 

Share this post


Link to post
Share on other sites
jguinch

Try with this code :

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

Global $FFmpegDir = @ScriptDir & "\www\bin\"

$sCommands = "FFmpeg -list_devices true -f dshow -i dummy"
$iPID = Run("cmd /c " & $sCommands, $FFmpegDir , @SW_Hide , $stderr_child + $stdout_child)

$sOutput = ""
While 1
    $sOutput &= StderrRead($iPID)
    If @error Then ExitLoop
WEnd
Local $sVideoDevices, $sAudioDevices
Local $iVideoLine = 0, $iAudioLine = 0

$aLines = StringRegExp($sOutput, '\[[^\]]+\]\h+(?|(DirectShow (?:audio|video) devices)|"([^"]+)"|(Could not enumerate\N+))', 3)
For $i = 0 To UBound($aLines) - 1
    If StringInStr($aLines[$i], "DirectShow video devices") Then
        $iVideoLine = 1
        $iAudioLine = 0
        ContinueLoop
    ElseIf StringInStr($aLines[$i], "DirectShow audio devices") Then
        $iAudioLine = 1
        $iVideoLine = 0
        ContinueLoop
    EndIf
    
    If $iVideoLine Then $sVideoDevices &= $aLines[$i] & @CRLF
    If $iAudioLine Then $sAudioDevices &= $aLines[$i] & @CRLF
Next

MsgBox(0, "DirectShow video devices", $sVideoDevices)
MsgBox(0, "DirectShow audio devices", $sAudioDevices)

Edit : I edited my code because when I tried the code on my computer an I saw this error :

[dshow @ 02c09800] DirectShow video devices (some may be both video and audio devices)
[dshow @ 02c09800] Could not enumerate video devices (or none found).
[dshow @ 02c09800] DirectShow audio devices
[dshow @ 02c09800]  "Realtek Digital Input (Realtek "
[dshow @ 02c09800]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\Realtek Digital Input (Realtek "
dummy: Immediate exit requested

Edited by jguinch

Share this post


Link to post
Share on other sites
mikell

or this one  :D

$dStrings = '[dshow @ 02b85e40] DirectShow video devices' & @CRLF & _
           '[dshow @ 02b85e40]  "CyberLink Webcam Splitter"' & @CRLF & _
           '[dshow @ 02b85e40]  "USB Video Device"' & @CRLF & _
           '[dshow @ 02b85e40]  "IP Camera [JPEG/MJPEG]"' & @CRLF & _
           '[dshow @ 02b85e40]  "DroidCam"' & @CRLF & _
           '[dshow @ 02b85e40] DirectShow audio devices' & @CRLF & _
           '[dshow @ 02b85e40]  "USB Audio Device"' & @CRLF & _
           '[dshow @ 02b85e40]  "Realtek HD Audio Input"'

$nV = "DirectShow video devices"
$nA = "DirectShow audio devices"
$dev = StringRegExp($dStrings, '(?s)(' & $nV & '.*)(' & $nA & '.*)', 3)

$pattern = '(?m)(^[^"]+"?)|("(?=\R?))'
msgbox(0, $nV, $nV &@crlf&@crlf& StringRegExpReplace($dev[0], $pattern , ""))
msgbox(0, $nA, $nA &@crlf&@crlf& StringRegExpReplace($dev[1], $pattern , ""))

 

Edited by mikell
  • Like 1

Share this post


Link to post
Share on other sites
dynamitemedia

 

​heck yeah that was a lot cleaner code than mine...

i think the issue is the $dstrings wouldb ebe different on every pc so i went with just getting the $sOutput from the  StderrRead​

seems to work without the need for the $dDtrings

Share this post


Link to post
Share on other sites
iamtheky

#Include <Array.au3>

$dStrings = '[dshow @ 02b85e40] DirectShow video devices' & @CRLF & _
           '[dshow @ 02b85e40]  "CyberLink Webcam Splitter"' & @CRLF & _
           '[dshow @ 02b85e40]  "USB Video Device"' & @CRLF & _
           '[dshow @ 02b85e40]  "IP Camera [JPEG/MJPEG]"' & @CRLF & _
           '[dshow @ 02b85e40]  "DroidCam"' & @CRLF & _
           '[dshow @ 02b85e40] DirectShow audio devices' & @CRLF & _
           '[dshow @ 02b85e40]  "USB Audio Device"' & @CRLF & _
           '[dshow @ 02b85e40]  "Realtek HD Audio Input"'

$aSplit = stringsplit($dStrings , "DirectShow audio devices" & @CRLF , 3)

$aVideo = stringsplit($aSplit[0] , @CRLF , 3)
_ArrayDelete($aVideo , ubound($aVideo) - 1)
_ArrayDelete($aVideo , 0)

$aAudio = stringsplit($aSplit[1] , @CRLF , 3)


_ArrayDisplay($aVideo , "Video")
_ArrayDisplay($aAudio , "Audio")

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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

  • Similar Content

    • FroVN
      By FroVN
      i have a text : <Name>Jonh</Name>.<Age>15</Age>
      how i can get Jonh and 15 in one stringregexp? pls give me example
    • therks
      By therks
      I'm looking for a regex genius, cus I'm stumped when it comes to assertions.
      So what I have now, is this regular expression: ([^|=]+)=([^|]+)
      It takes a string (user input) of keys=values separated by pipes (ie: "param=value|param=value") and splits them into an array.
      Example:
      $vParamData = 'example=value|fruit=apple|phrase=Hello world' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; Result ; [0] => example ; [1] => value ; [2] => fruit ; [3] => apple ; [4] => phrase ; [5] => Hello world So that's working fine, but I'm wondering if there's also a way I could have this capture escaped pipes instead of splitting by them.
      ie:
      $vParamData = 'pipe test=this \| is a pipe|example=value' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; I'm getting this: ; [0] => pipe test ; [1] => this \ ; [2] => example ; [3] => value ; But I'd like a result like this: ; [0] => pipe test ; [1] => this \| is a pipe ; [2] => example ; [3] => value Is there some pattern that would accomplish this, or am I better off parsing it some other way?
    • Robinson1
      By Robinson1
      Well the plan is to use the power of regular expressions engine of AutoIT for patching binary data.
      Something like this: StringRegExp( $BinaryData,  "(?s)\x55\x8B.."
       
      <cut> ... Okay straight to question/problem
      ... certain bytes that are in the range from 0x80 to 0xA0 won't match.
      Hmm seem to be a char encoding problem. In detail these are 27 chars: 0x80, 0x82~8C, 0x8E, 0x91~9C, 0x9E,0x9F
      Here's a small code snippet to explore / explain this problem:
      #include "StringConstants.au3" $TestData = BinaryToString("0x7E7F808182") ;Okay $match = StringRegExp( $TestData ,'\x7E' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x7F' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x80' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x81' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x82' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;~ output: ;~ @extended = 2 $match = ;~ @extended = 3 $match = ;~ @extended = 0 $match = 1 ;~ @extended = 5 $match = ;~ @extended = 0 $match = 1 Hmm what to do? Go back and use the 'numberstring monster' implementation or just omit that range of 'unsafe bytes'. What is the root of this problem?
      Any idea how to fix this?
       
      Update: Okay I know a byte is not a character.
      But StringRegExp operates on String and so character level.
      Okay as long as you stay at Ansi encoding and only use /x00 - /X7F in the search pattern using  StringRegExp works well to search for binary data.
      What bytes can be matched that are in the range from /X7F - /xFF is also depending on the code page.
      So this avoid to search for bytes in the range from 0x80-0xa0 only applies to Germany.
      I just change this country setting:

      to Thai and now near all bytes from /X7F - /xFF fails to match.
    • Carm01
      By Carm01
      Hello,
      I have spent the past day fooling with StringRegExp to no avail attempting to get what would be a simple solution to an issue using StringRegExp.
      I will post the code in a sec. The string 'Java x Update y' where x and y are numeric values ONLY if a letter is mixed in anywhere then it should fail. I have been able to successfully deal with the x value so if x = 1234 or a1234 or 1a234 or 1234a would result in a fail if 'a' was in the string. However, when y = 1a234 then I get an output of 1 and when y = 1234a then the output = 1234 when both should fail. I am probably overlooking something simple and in looking through all the material and experimenting I am unable to figure it out and my experience with stringregexp and trying to find examples of this proved difficult. If someone could assist or point me to a thread ? Here is my code ; prob a simple fix. I am also trying to avoid white spaces.
      Thanks in advance
      #include <array.au3> $aArray = StringRegExp('Java 3009 Update 1a21', '(?i)Java (\d+) Update (\d+)', $STR_REGEXPARRAYGLOBALMATCH) If @error Then Exit _ArrayDisplay($aArray)  
    • VIP
      By VIP
      Need help to make function better  with full infomation
      #include <Array.au3> #include <File.au3> _TEST(@ScriptFullPath) _TEST("A:") _TEST("A:\B.c") _TEST("D:\E\F\") _TEST("G:\H/../J.k/") _TEST("M:\N\k..J.k") _TEST("D:\E\F\..\G\G\I..J.K.M") Func _TEST($sFilePath) Local $sDrive = "", $sFullPathDir = "", $sDirPath = "", $sDirName = "", $sFileName = "", $sFileNameExt = "", $sExtension = "", $sExt = "" Local $aPathSplit = _PathSplitByRef($sFilePath, $sDrive, $sFullPathDir, $sDirPath, $sDirName, $sFileName, $sFileNameExt, $sExtension, $sExt) ConsoleWrite("!Path IN : " & $sFilePath & @CRLF) ; C:\Windows\System32\etc\hosts.exe ConsoleWrite("- Driver : " & $sDrive & @CRLF) ; C: ConsoleWrite("- DirPath : " & $sFullPathDir & @CRLF) ; C:\Windows\System32\etc\etc ConsoleWrite("- DirPath : " & $sDirPath & @CRLF) ; \Windows\System32\etc\ ConsoleWrite("- DirName : " & $sDirName & @CRLF) ; etc ConsoleWrite("- FileName : " & $sFileName & @CRLF) ; hosts ConsoleWrite("- FileNameExt: " & $sFileNameExt & @CRLF) ; hosts.exe ConsoleWrite("- Extension : " & $sExtension & @CRLF) ; .exe ConsoleWrite("- Ext : " & $sExt & @CRLF & @CRLF) ; exe ;~ ConsoleWrite("!Path IN : " & $aPathSplit[0] & @CRLF) ; C:\Windows\System32\etc\hosts.exe ;~ ConsoleWrite("- Driver : " & $aPathSplit[1] & @CRLF) ; C: ;~ ConsoleWrite("- DirPath : " & $aPathSplit[2] & @CRLF) ; C:\Windows\System32\etc\etc ;~ ConsoleWrite("- DirPath : " & $aPathSplit[3] & @CRLF) ; \Windows\System32\etc\ ;~ ConsoleWrite("- DirName : " & $aPathSplit[4] & @CRLF) ; etc ;~ ConsoleWrite("- FileName : " & $aPathSplit[5] & @CRLF) ; hosts ;~ ConsoleWrite("- FileNameExt: " & $aPathSplit[6] & @CRLF) ; hosts.exe ;~ ConsoleWrite("- Extension : " & $aPathSplit[7] & @CRLF) ; .exe ;~ ConsoleWrite("- Ext : " & $aPathSplit[8] & @CRLF) ; exe ;~ _ArrayDisplay($aPathSplit, "_PathSplit of " & $sFilePath) EndFunc ;==>_TEST Func _PathSplitByRef($sFilePath, ByRef $sDrive, ByRef $sFullPathDir, ByRef $sDirPath, ByRef $sDirName, ByRef $sFileName, ByRef $sFileNameExt, ByRef $sExtension, ByRef $sExt) If StringInStr($sFilePath,"..") Then $sFilePath=_PathFull($sFilePath) Local $aPartOfPath=StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH) ;~ If @error Then ReDim $aPartOfPath[9] ;~ $aPartOfPath[0] = $sFilePath ;~ EndIf $aPartOfPath[0] = $sFilePath ; C:\Windows\System32\etc\hosts.exe $sDrive = $aPartOfPath[1] ; C: $sFullPathDir = $aPartOfPath[1] & $aPartOfPath[2] ; C:\Windows\System32\etc If StringLeft($aPartOfPath[2], 1) == "/" Then $sDirPath = StringRegExpReplace($aPartOfPath[2], "\h*[\/\\]+\h*", "\/") Else $sDirPath = StringRegExpReplace($aPartOfPath[2], "\h*[\/\\]+\h*", "\\") EndIf $aPartOfPath[2] = $sFullPathDir ; C:\Windows\System32\etc $sDirName=StringReplace($sDirPath,"\","") $sDirName=StringReplace($sDirPath,"/","") $sFileName = $aPartOfPath[3] ; hosts $aPartOfPath[5] = $sFileName ; hosts $sExtension = $aPartOfPath[4] ; .exe $aPartOfPath[7] = $sExtension ; .exe $aPartOfPath[3] = $sDirPath ; \Windows\System32\etc\ $aPartOfPath[4] = $sDirName ; etc $aPartOfPath[6] = $sFileName & $sExtension ; hosts.exe $sFileNameExt = $aPartOfPath[6] ; hosts.exe $sExt = StringReplace($sExtension,".","") ; exe $aPartOfPath[8] = $sExt ; exe Return $aPartOfPath EndFunc ;==>_PathSplitByRef  
×