Extracting values from a char variable

3 visualizzazioni (ultimi 30 giorni)
Adam
Adam il 14 Dic 2022
Commentato: Stephen23 il 14 Dic 2022
I am running FFmpeg through the system() function and the output is being stored within a char variable cmdout. I have added minimal reproducible code.
cmdout = "Guessed Channel Layout for Input Stream #0.0 : monoInput #0, wav, from 'testwav.wav': Duration: 00:00:02.92, bitrate: 256 kb/s Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/sStream mapping: Stream #0:0 -> #0:0 (pcm_s16le (native) -> opus (libopus))Press [q] to stop, [?] for helpOutput #0, opus, to 'test.opus': Metadata: encoder : Lavf59.27.100 Stream #0:0: Audio: opus, 16000 Hz, mono, s16, 8 kb/s Metadata: encoder : Lavc59.37.100 libopussize= 0kB time=00:00:00.12 bitrate= 8.8kbits/s speed=N/A size= 13kB time=00:00:02.92 bitrate= 35.8kbits/s speed= 241x video:0kB audio:11kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 12.397972%bench: utime=0.000s stime=0.016s rtime=0.012sbench: maxrss=14764kB";
This is how it displayed on the command window (without the %).
%Guessed Channel Layout for Input Stream #0.0 : mono
%Input #0, wav, from 'testwav.wav':
% Duration: 00:00:02.92, bitrate: 256 kb/s
% Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
%Stream mapping:
% Stream #0:0 -> #0:0 (pcm_s16le (native) -> opus (libopus))
%Press [q] to stop, [?] for help
%Output #0, opus, to 'test.opus':
% Metadata:
% encoder : Lavf59.27.100
% Stream #0:0: Audio: opus, 16000 Hz, mono, s16, 8 kb/s
% Metadata:
% encoder : Lavc59.37.100 libopus
%size= 0kB time=00:00:00.12 bitrate= 8.8kbits/s speed=N/A
%size= 13kB time=00:00:02.92 bitrate= 35.8kbits/s speed= 241x
%video:0kB audio:11kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 12.397972%
%bench: utime=0.000s stime=0.016s rtime=0.012s
%bench: maxrss=14764kB
I am trying to extract out the values of the following
  • 'Duration: 00:00:02.92'
  • 'bitrate: 256'
  • 'Stream #0:0: Audio: opus, 16000 Hz, mono, s16, 8 kb/s'
  • 'encoder : Lavc59.37.100 libopus'
  • 'utime=0.000'
  • 'stime=0.016'
  • 'rtime=0.012'
So far I have I am currently using the regexp() function to extract out the values into a struct.
Values = regexp(cmdout,'(?<utime>utime=.{5})','names'); % Only one working so far
for k = 1:length(Values)
disp(Values(k))
end
utime: "utime=0.000"
I have been unable to add multiple regex statements without it failing to find anything. I was also not sure if it was possible to have the struct just store the actual value e.g. utime: '0.000' rather than how it currently also contains the text.
  5 Commenti
Adam
Adam il 14 Dic 2022
The one that contains "Audio: opus" after it.
Stephen23
Stephen23 il 14 Dic 2022
And how to distinguish between the lines containing "encoder :" ?

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 14 Dic 2022
Modificato: Stephen23 il 14 Dic 2022
str = fileread('text.txt')
str =
'Guessed Channel Layout for Input Stream #0.0 : mono Input #0, wav, from 'testwav.wav': Duration: 00:00:02.92, bitrate: 256 kb/s Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s Stream mapping: Stream #0:0 -> #0:0 (pcm_s16le (native) -> opus (libopus)) Press [q] to stop, [?] for help Output #0, opus, to 'test.opus': Metadata: encoder : Lavf59.27.100 Stream #0:0: Audio: opus, 16000 Hz, mono, s16, 8 kb/s Metadata: encoder : Lavc59.37.100 libopus size= 0kB time=00:00:00.12 bitrate= 8.8kbits/s speed=N/A size= 13kB time=00:00:02.92 bitrate= 35.8kbits/s speed= 241x video:0kB audio:11kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 12.397972% bench: utime=0.000s stime=0.016s rtime=0.012s bench: maxrss=14764kB'
In three separate REGEXP calls (combining may be possible, but only with significant effort and complexity):
rx1 = "(Duration|bitrate|encoder)\s*:\s+([^,\n\r]+)";
tk1 = regexp(str,rx1,'tokens');
tk1 = vertcat(tk1{:})
tk1 = 4×2 cell array
{'Duration'} {'00:00:02.92' } {'bitrate' } {'256 kb/s' } {'encoder' } {'Lavf59.27.100' } {'encoder' } {'Lavc59.37.100 libopus'}
rx2 = "(Stream\s+\S+\s+Audio:)\s+(opus[^\n\r]+)";
tk2 = regexp(str,rx2,'tokens','once')
tk2 = 1×2 cell array
{'Stream #0:0: Audio:'} {'opus, 16000 Hz, mono, s16, 8 kb/s'}
rx3 = "([a-z]time)=(\d\.?\d*)";
tk3 = regexp(str,rx3,'tokens');
tk3 = vertcat(tk3{:})
tk3 = 3×2 cell array
{'utime'} {'0.000'} {'stime'} {'0.016'} {'rtime'} {'0.012'}
  2 Commenti
Adam
Adam il 14 Dic 2022
Thanks for the help. Is there any interactive or alternative sources for understanding the regexp commands. Something like Regex 101 but for MATLAB
Stephen23
Stephen23 il 14 Dic 2022
"Something like Regex 101 but for MATLAB"
There are several interactive tools on MATLAB FEX:
For example, my one is intended to be the simplest interactive tool on FEX:
Not as fancy as Regex 101, but it might help you enough.
"...alternative sources for understanding the regexp commands"
Repeated reading of the documentation. And when I write "repeated", I don't mean twice.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Audio I/O and Waveform Generation in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by