Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Aarrtteemm123 opened this issue Oct 27, 2020 · 5 comments
Open

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

Aarrtteemm123 opened this issue Oct 27, 2020 · 5 comments

Comments

@Aarrtteemm123
Copy link

Aarrtteemm123 commented Oct 27, 2020

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
Copy link
Member

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

@Aarrtteemm123
Copy link
Author

import sox

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

@lostanlen
Copy link
Member

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
Copy link
Author

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
Copy link

Sean-McCarty commented Nov 24, 2020

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])`

ktrzcinx pushed a commit to ktrzcinx/pysox that referenced this issue May 5, 2021
Previous version of parsing was unable to get valid content
eg. when `output` ends with '\r' character what is possible
in cross platform environment. New parser version is resistant
for any line ending.
Moreover parser is insensistive for prefix letter size, see bug:
marl#119 (comment)

Signed-off-by: Karol Trzcinski <k.trzcinski95@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants