Extracting values from a char variable
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
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
Risposta accettata
Stephen23
il 14 Dic 2022
Modificato: Stephen23
il 14 Dic 2022
str = fileread('text.txt')
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{:})
rx2 = "(Stream\s+\S+\s+Audio:)\s+(opus[^\n\r]+)";
tk2 = regexp(str,rx2,'tokens','once')
rx3 = "([a-z]time)=(\d\.?\d*)";
tk3 = regexp(str,rx3,'tokens');
tk3 = vertcat(tk3{:})
2 Commenti
Stephen23
il 14 Dic 2022
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Audio I/O and Waveform Generation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!