#1 Le 14/03/2015, à 22:59
- totola
python: récupérer le nombre avant % dans un fichier
bonsoir,
la commande :
arecord -D hw:0 -c 2 -d 0 -f S16_LE -vvv /dev/null | grep %
me génère une sortie de ce type:
$ arecord -D hw:0 -c 2 -d 0 -f S16_LE -vvv /dev/null | grep %
Capture WAVE '/dev/null' : Signed 16 bit Little Endian, Frquence 8000 Hz, Stro
Avertissement: la frquence n'est pas prcise (demand = 8000Hz,
obtenu = 44100Hz)
veuillez essayez le greffon de branchement
Hardware PCM card 0 'HDA Intel' device 0 subdevice 0
Its setup is:
stream : CAPTURE
access : RW_INTERLEAVED
format : S16_LE
subformat : STD
channels : 2
rate : 44100
exact rate : 44100 (44100/1)
msbits : 16
buffer_size : 16384
period_size : 4096
period_time : 92879
tstamp_mode : NONE
period_step : 1
avail_min : 4096
period_event : 0
start_threshold : 1
stop_threshold : 16384
silence_threshold: 0
silence_size : 0
boundary : 1073741824
appl_ptr : 0
hw_ptr : 0
Pic max. (8192 chantillons): 0x00000071 # 0%
Pic max. (8192 chantillons): 0x00000056 # 0%
Pic max. (8192 chantillons): 0x00000060 # 0%
Pic max. (8192 chantillons): 0x000000b7 # 0%
Pic max. (8192 chantillons): 0x00000129 # 0%
Pic max. (8192 chantillons): 0x000000de # 0%
Pic max. (8192 chantillons): 0x00000047 # 0%
Pic max. (8192 chantillons): 0x00000056 # 0%
Pic max. (8192 chantillons): 0x00000095 # 0%
Pic max. (8192 chantillons): 0x000000a1 # 0%
Pic max. (8192 chantillons): 0x0000011b # 0%
Pic max. (8192 chantillons): 0x000000a7 # 0%
Pic max. (8192 chantillons): 0x0000016f # 1%
Pic max. (8192 chantillons): 0x0000009a # 0%
Pic max. (8192 chantillons): 0x00000074 # 0%
Pic max. (8192 chantillons): 0x000000fb # 0%
Pic max. (8192 chantillons): 0x00000051 # 0%
Pic max. (8192 chantillons): 0x00000073 # 0%
Pic max. (8192 chantillons): 0x00000088 # 0%
Pic max. (8192 chantillons): 0x0000022d # 1%
Pic max. (8192 chantillons): 0x00000126 # 0%
Pic max. (8192 chantillons): 0x00000109 # 0%
Pic max. (8192 chantillons): 0x00000211 # 1%
Pic max. (8192 chantillons): 0x000001bf # 1%
Pic max. (8192 chantillons): 0x00000048 # 0%
Pic max. (8192 chantillons): 0x000000c4 # 0%
Pic max. (8192 chantillons): 0x000000f5 # 0%
Pic max. (8192 chantillons): 0x000003f9 # 3%
Pic max. (8192 chantillons): 0x000003f3 # 3%
Pic max. (8192 chantillons): 0x00000327 # 2%
Pic max. (8192 chantillons): 0x0000138c #### 15%
Pic max. (8192 chantillons): 0x000015ca #### 17%
J'aimerais juste récupérer la valeur avant le %
Je vois pas trop comment faire
Merci
Hors ligne
#2 Le 15/03/2015, à 00:07
- Gaara
Re : python: récupérer le nombre avant % dans un fichier
Bonsoir,
En fait je crois qu'il faut écrire cette sortie dans un fichier temp, puis python lit ligne par ligne ce fichier (en boucle), et quand il tombe sur par ex. "Pic max. (8192 chantillons)", il retourne la valeur du pourcentage.
C'est avec cette méthode que j'ai réussi à sortir un pourcentage d'apt.
Il y a peut-être (et sûrement) mieux, mais celle-ci fonctionne
Kubuntu 18.04 x64
Un terminal tactile Raspberry Pi et Odroid
<code>zenity --question --title "Alert" --text "Microsoft Windows has been found! Would you like to remove it?"</code>
Hors ligne
#3 Le 15/03/2015, à 16:51
- pingouinux
Re : python: récupérer le nombre avant % dans un fichier
Bonjour,
Si tu obtiens ce résultat avec la commande que tu montres, c'est que arecord écrit sur stderr en plus de stdout.
Pour n'obtenir que les nombres qui précèdent immédiatement % :
une_commande_qui_génère_des_lignes_avec_pourcent | grep -Po '[0-9]+(?=%)'
Hors ligne
#4 Le 15/03/2015, à 18:32
- totola
Re : python: récupérer le nombre avant % dans un fichier
Bonjour et merci de la réponse..
J'obtiens ça:
$ arecord -D hw:1 -c 2 -d 0 -f S16_LE -vvv /dev/null | grep -Po '[0-9]+(?=%)'
Capture WAVE '/dev/null' : Signed 16 bit Little Endian, Fréquence 8000 Hz, Stéréo
Avertissement: la fréquence n'est pas précise (demandé = 8000Hz,
obtenu = 44100Hz)
veuillez essayez le greffon de branchement
Hardware PCM card 1 'HD-Audio Generic' device 0 subdevice 0
Its setup is:
stream : CAPTURE
access : RW_INTERLEAVED
format : S16_LE
subformat : STD
channels : 2
rate : 44100
exact rate : 44100 (44100/1)
msbits : 16
buffer_size : 16384
period_size : 4096
period_time : 92879
tstamp_mode : NONE
period_step : 1
avail_min : 4096
period_event : 0
start_threshold : 1
stop_threshold : 16384
silence_threshold: 0
silence_size : 0
boundary : 4611686018427387904
appl_ptr : 0
hw_ptr : 0
2%
3%
2%
2%
1%
2%
1%
2%
1%
2%
2%
2%
5%
2%
1%
1%
2%
2%
2%
2%
1%
2%
2%
2%
Hors ligne
#5 Le 15/03/2015, à 18:54
- pingouinux
Re : python: récupérer le nombre avant % dans un fichier
Pour n'avoir que les lignes qui contiennent un %, je pense que ceci devrait convenir (du moins aurait dû) :
arecord -D hw:1 -c 2 -d 0 -f S16_LE -vvv /dev/null 2>/dev/null | grep -Po '[0-9]+(?=%)'
En revanche, je ne comprends pas pourquoi les % ne disparaissent pas.
Essaye aussi ceci :
arecord -D hw:1 -c 2 -d 0 -f S16_LE -vvv /dev/null 2>/dev/null | sed -nr '/%/s/.*([0-9]+)%/\1/p'
Hors ligne
#6 Le 15/03/2015, à 18:58
- totola
Re : python: récupérer le nombre avant % dans un fichier
Oui ça marche avec sed -nr...
Merci!
Hors ligne
#7 Le 15/03/2015, à 19:32
- pingouinux
Re : python: récupérer le nombre avant % dans un fichier
Tu peux même simplifier un peu
arecord -D hw:1 -c 2 -d 0 -f S16_LE -vvv /dev/null 2>/dev/null | sed -nr 's/.*([0-9]+)%/\1/p'
Hors ligne