marl / pysox

Python wrapper around sox.
BSD 3-Clause "New" or "Revised" License
517 stars 80 forks source link

ValueError: could not convert string to float: '1.41M' #119

Open Aarrtteemm123 opened 4 years ago

Aarrtteemm123 commented 4 years ago

import sox

sox.file_info.info('inputs files/input2.wav') // ValueError

Full error Traceback (most recent call last): File "C:/Users/USER_ARTEM/Desktop/Python/test/test2.py", line 4, in sox.file_info.info('inputs files/input2.wav') File "C:\Users\USER_ARTEM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sox\file_info.py", line 357, in info 'bitrate': bitrate(filepath), File "C:\Users\USER_ARTEM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sox\file_info.py", line 64, in bitrate return float(output[:-1]) ValueError: could not convert string to float: '1.41M'

lostanlen commented 4 years ago

Hello @Aarrtteemm123. Can you please give us the output of soxi for the same file?

Aarrtteemm123 commented 4 years ago

import sox

print(sox.file_info.soxi('inputs files/input2.wav','B')) # 1.41M

lostanlen commented 4 years ago

Sorry for the confusion. What i was trying to ask what: what happens when you run soxi on this file from the command line prompt, outside of a Python session?

Aarrtteemm123 commented 4 years ago

C:\Users\USER_ARTEM\Desktop\Python\test>sox --i input.wav

Input File : 'input.wav' Channels : 2 Sample Rate : 44100 Precision : 16-bit Duration : 00:00:10.00 = 441000 samples = 750 CDDA sectors File Size : 1.76M Bit Rate : 1.41M Sample Encoding: 16-bit Signed Integer PCM

C:\Users\USER_ARTEM\Desktop\Python\test>soxi input.wav 'soxi' is not recognized as an internal or external command, operable program or batch file.

C:\Users\USER_ARTEM\Desktop\Python\test>

Sean-McCarty commented 3 years ago

I'm also getting this error trying to use sox.file_info.info, because the greek_prefixes string isn't being parsed correctly. In my case, the bitrate value from the MP3 file is 64.1K (upper case) instead of 64.1k (lower case), which it seems was expected. (See the last if...else block in the code below). I would suggesting the following:

  1. Convert greek_prefixes to a list instead of a string or use regex instead
  2. Use all uppercase letters instead of a mix
  3. Deal with the case using output[-1].upper()

Source code from sox.file_info (https://pysox.readthedocs.io/en/latest/_modules/sox/file_info.html): ` def bitrate(input_filepath):

Bit rate averaged over the whole file.
Expressed in bytes per second (bps), or None if not applicable.

Parameters
----------
input_filepath : str
    Path to audio file.

Returns
-------
bitrate : float or None
    Bit rate, expressed in bytes per second.
    Returns None if not applicable.

validate_input_file(input_filepath)
output = soxi(input_filepath, 'B')
# The characters below stand for kilo, Mega, Giga, etc.
greek_prefixes = '\0kMGTPEZY'
if output == "0":
    logger.warning("Bit rate unavailable for %s", input_filepath)
    return None
elif output[-1] in greek_prefixes:
    multiplier = 1000.0**(greek_prefixes.index(output[-1]))
    return float(output[:-1])*multiplier
else:
    return float(output[:-1])`