1

This code works:

import wave

f1 = wave.open(file1, "r")
num_channels_file1 = int(f1.getnchannels())

but it does not work when reading a wav file with a diff bitrate or other property. I can't figure out the difference btw wav files or other intricacies, I just need a way to check if a wav file has 2 channels.

ERJAN
  • 23,696
  • 23
  • 72
  • 146
  • I find your question confusing. "**I don't wanna figure out the difference btw wav files**" - who said you would? `wave` is much more than a `.wav` file compare and it's the right tool to tell you how many channels the `wav` file has. – Pedro Lobito Apr 23 '20 at 21:24
  • @PedroLobito thx u , but i just have this "runtime error" that i can't fix when reading wav file – ERJAN Apr 23 '20 at 21:25
  • @PedroLobito, if i remove wav.open() it works ok, but i need to detect if it has 2 channels or 1 – ERJAN Apr 23 '20 at 21:31

2 Answers2

2

The number of channels is a two-byte integer at position 0x16 in the RIFF header, so you can just read it directly:

import struct

with open(file1, 'rb') as f1:
    header_beginning = f1.read(0x18)
    num_channels_file1, = struct.unpack_from('<H', header_beginning, 0x16)

If you're interested in supporting big-endian WAV files (which I think are uncommon), you can detect them by reading the first four bytes of the file ("RIFF" or "XFIR" for little-endian, and "FFIR" or "RIFX" for big-endian). Then, for big-endian files, switch from '<H' to '>H' when reading the number of channels.

RoadrunnerWMC
  • 693
  • 4
  • 9
  • how would i know this without u? – ERJAN Apr 23 '20 at 21:33
  • I just googled "wav header" and looked at the table on this page to find the number-of-channels value: http://www.topherlee.com/software/pcm-tut-wavformat.html – RoadrunnerWMC Apr 23 '20 at 21:35
  • not it's not working.. when i give file in postman , it gives this error - TypeError: expected str, bytes or os.PathLike object, not FileStorage – ERJAN Apr 23 '20 at 21:35
  • There's not really context or information for me to help with that. It sounds like an unrelated problem, though. – RoadrunnerWMC Apr 23 '20 at 21:38
  • i m using postman to build a voice verification project. wav files are customer support calls. conversations. I need a 10sec voice of single customer. so if i M given a conversation - i need to detect it – ERJAN Apr 23 '20 at 21:41
  • You edited the error you said you got -- now it *definitely* sounds like an unrelated problem. I still can't diagnose it without context, and this is getting really off-topic from the original question. Best of luck, though! – RoadrunnerWMC Apr 23 '20 at 21:48
1

SOX, "the Swiss Army knife of sound processing programs", is also an option:

from sox import file_info

ch = file_info.channels("WAV_2MG.wav") # https://file-examples.com/wp-content/uploads/2017/11/file_example_WAV_2MG.wav
print(ch)
# 2

Python Install

pip3 install sox

Make sure SOX is installed on the system, if not:

apt-get install sox # ubuntu
yum install sox # redhat/fedora/centos
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • still gives me error "TypeError: stat: path should be string, bytes, os.PathLike or integer, not FileStorage" – ERJAN Apr 23 '20 at 21:47
  • i have uploaded the pic of postman screen here https://ufile.io/54b7d6bf – ERJAN Apr 23 '20 at 21:47
  • That's strange, because I've just tested the code on a CentOS7 and it worked. Which OS are using? – Pedro Lobito Apr 23 '20 at 21:48
  • ubuntu. it's strange too, it 's actually working - but not as expected – ERJAN Apr 23 '20 at 21:50
  • basically if i upload 2 files - it shows digit 2 for channels, if i upload 1 conversation file with 2 channels - it just gives me 1 for channels, but it should be 2 channels! – ERJAN Apr 23 '20 at 21:50
  • The problem seems related to the webapp and not the audio application. Try running the script on a command line to be sure where the problem is. – Pedro Lobito Apr 23 '20 at 21:52
  • that first error with "file path storage" is gone - i got the file name thru file1.filename field – ERJAN Apr 23 '20 at 21:53
  • i can actually send filenames of the files i upload thru postman.. but now the problem is diff... damn – ERJAN Apr 23 '20 at 21:54
  • i did ur advice: python basic_info.py conversation_2_10_sec.wav for file :conversation_2_10_sec.wav num of channels is: 2 – ERJAN Apr 23 '20 at 21:57
  • so it does work and it does show there are actually 2 channels... – ERJAN Apr 23 '20 at 21:57
  • Perfect! Glad you found a solution :) – Pedro Lobito Apr 23 '20 at 22:01