Error trying to use AudioSegment for .wav files - python

I'm trying to iterate through all the .wav files in a folder "audios", but I receive the following error. I found similar questions that were solved by installing ffmpeg, but that didn't help.
FileNotFoundError Traceback (most recent call last)
<ipython-input-24-29ba732186ac> in <module>
1 for audio_file in os.listdir(base_path+"audios"):
2 # read wav audio file
----> 3 audio = AudioSegment.from_wav(audio_file)
4
5 # pass audio file, start time, end time & chunk path to create chunk
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\audio_segment.py in from_wav(cls, file, parameters)
806 #classmethod
807 def from_wav(cls, file, parameters=None):
--> 808 return cls.from_file(file, 'wav', parameters=parameters)
809
810 #classmethod
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\audio_segment.py in from_file(cls, file, format, codec, parameters, start_second, duration, **kwargs)
649 except TypeError:
650 filename = None
--> 651 file, close_file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)
652
653 if format:
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\utils.py in _fd_or_path_or_tempfile(fd, mode, tempfile)
58
59 if isinstance(fd, basestring):
---> 60 fd = open(fd, mode=mode)
61 close_fd = True
62
FileNotFoundError: [Errno 2] No such file or directory: 'name_of_file.wav'

os.listdir doesn't return the full paths of files in the directory you give it, just the names they have within that directory. You will need to prepend this directory name to the filename you pass to AudioSegment.from_wav.
Try replacing the line
audio = AudioSegment.from_wav(audio_file)
with
audio = AudioSegment.from_wav(os.path.join(base_path+"audios", audio_file))

Related

For loop crashing on speech_recognition

Python Newbie trying to teach myself how to use Python to run speech_recognition, and Im not having great luck.
The code below runs once and correctly converts a wav file to text, but then it crashes before running the remaining 2 wav files in my S3 bucket. The files are absolutely there:
OSR_us_000_0010_8k.wav
OSR_us_000_0011_8k.wav
OSR_us_000_0012_8k.wav
I could use some help fixing it.
Thanks in Advance.
import boto3
import speech_recognition as sr
r = sr.Recognizer()
session = boto3.client('s3',
aws_access_key_id= XXXX,
aws_secret_access_key=XXXX,
region_name='XXXX')
my_bucket = s3.Bucket(mys3bucket)
for my_bucket_object in my_bucket.objects.all():
with sr.AudioFile(my_bucket_object.key) as source:
print(my_bucket_object.key)
audio_data = r.record(source)
text = r.recognize_google(audio_data)
print(text)
OSR_us_000_0010_8k.wav<br>
Birch canoe slid on the smooth plank glue the sea to a dark blue background it is easy to tell the depth of a well these day the chicken leg of a variegated rice is often served in roundels the juice of lemons mix find the boxes on the side the pump truck the ha grimstead topcon and garbage for hours of Citi workspace a large-sized and stockings in the hearts of cell
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-4-385959f26678> in <module>
14
15 for my_bucket_object in my_bucket.objects.all():
---> 16 with sr.AudioFile(my_bucket_object.key) as source:
17 print(my_bucket_object.key)
18 audio_data = r.record(source)
~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/site-packages/speech_recognition/__init__.py in __enter__(self)
201 try:
202 # attempt to read the file as WAV
--> 203 self.audio_reader = wave.open(self.filename_or_fileobject, "rb")
204 self.little_endian = True # RIFF WAV is a little-endian format (most ``audioop`` operations assume that the frames are stored in little-endian form)
205 except (wave.Error, EOFError):
~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/wave.py in open(f, mode)
508 mode = 'rb'
509 if mode in ('r', 'rb'):
--> 510 return Wave_read(f)
511 elif mode in ('w', 'wb'):
512 return Wave_write(f)
~/anaconda3/envs/mxnet_latest_p37/lib/python3.7/wave.py in __init__(self, f)
158 self._i_opened_the_file = None
159 if isinstance(f, str):
--> 160 f = builtins.open(f, 'rb')
161 self._i_opened_the_file = f
162 # else, assume it is an open file object already
FileNotFoundError: [Errno 2] No such file or directory: 'OSR_us_000_0011_8k.wav'

Upload large file (>100 MB) directly to github with pygithub

I am using pyGitHub to upload files to my repo, however some of the files are so large that the server connection times out. My code to upload/overwrite a file from a folder is:
def commit(folder):
foldername = folder.split("/")[-1]
onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
repo = g.get_repo(user.login+"/My-repo")
all_files = []
contents = repo.get_contents("")
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
contents.extend(repo.get_contents(file_content.path))
else:
file = file_content
all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
body = '''
Line 1: Message
Line 2: Sample Text
Line 3: yet another line
'''
for i in onlyfiles:
print(i)
input_file = open(folder + "/" + i, "rb")
data = input_file.read()
input_file.close()
if not(f"{foldername}/{i}" in all_files):
repo.create_file(f"{foldername}/{i}", "Created building data", data)
else:
file = repo.get_contents(f"{foldername}/{i}")
repo.update_file(file.path, "Updated information", data, file.sha)
This code works for files <25mb, but for larger ones I get the error:
---------------------------------------------------------------------------
GithubException Traceback (most recent call last)
<ipython-input-9-7d41473c81a0> in <module>()
79
80
---> 81 commit(str("/content/"+dirname))
3 frames
<ipython-input-9-7d41473c81a0> in commit(folder)
72 input_file.close()
73 if not(f"{foldername}/{i}" in all_files):
---> 74 repo.create_file(f"{foldername}/{i}", "Created building data", data)
75 else:
76 file = repo.get_contents(f"{foldername}/{i}")
/usr/local/lib/python3.7/dist-packages/github/Repository.py in create_file(self, path, message, content, branch, committer, author)
2091 "PUT",
2092 f"{self.url}/contents/{urllib.parse.quote(path)}",
-> 2093 input=put_parameters,
2094 )
2095
/usr/local/lib/python3.7/dist-packages/github/Requester.py in requestJsonAndCheck(self, verb, url, parameters, headers, input)
353 return self.__check(
354 *self.requestJson(
--> 355 verb, url, parameters, headers, input, self.__customConnection(url)
356 )
357 )
/usr/local/lib/python3.7/dist-packages/github/Requester.py in __check(self, status, responseHeaders, output)
376 output = self.__structuredFromJson(output)
377 if status >= 400:
--> 378 raise self.__createException(status, responseHeaders, output)
379 return responseHeaders, output
380
GithubException: 502 {"message": "Server Error"}
I am aware that the file upload limit for github is 25MB, but apparently files up to 100MB can be uploaded via the command line. How would I upload files larger than this to GitHub using pyGitHub? The file is zipped, so it really is as small as it can be, but is still ~150MB. Is this doable? If not, is there a way to reference a larger file in github which I can upload elsewhere? I am using Google Colab in case anyone is wondering.

Errno 2 No such file or directory:

I am using jupyter notebook (python 3.8 both from anaconda3) and following this post, cells 84 and 85 are resulting in the traceback and followed the advice of
FileNotFoundError Traceback (most recent call last)
<ipython-input-15-9cdebd0bb247> in <module>
2
3
----> 4 create_wordcloud(tw_list["text"].values)
<ipython-input-14-524a73dcd1e0> in create_wordcloud(text)
2
3 def create_wordcloud(text):
----> 4 mask = np.array(Image.open("cloud.png"))
5 stopwords = set(STOPWORDS)
6 wc = WordCloud(background_color="white",
~/opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode, formats)
2889
2890 if filename:
-> 2891 fp = builtins.open(filename, "rb")
2892 exclusive_fp = True
2893
FileNotFoundError: [Errno 2] No such file or directory: 'cloud.png'
following this i found advice (the link evades me but its somewhere on this site to change from PIL import image to import PIL.image in cell 2 and add
from IPython.display import Image
Image(filename='cloud.png')
still resulting in a similar, but longer traceback
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-16-8c5d56ae9874> in <module>
1 #Creating wordcloud for all tweets
2 from IPython.display import Image
----> 3 Image(filename='cloud.png')
4
5 create_wordcloud(tw_list["text"].values)
~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/display.py in
__init__(self, data, url, filename, format, embed, width, height, retina,
unconfined, metadata)
1222 self.retina = retina
1223 self.unconfined = unconfined
-> 1224 super(Image, self).__init__(data=data, url=url, filename=filename,
1225 metadata=metadata)
1226
~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/display.py in
__init__(self, data, url, filename, metadata)
628 self.metadata = {}
629
--> 630 self.reload()
631 self._check_data()
632
~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/display.py in
reload(self)
1254 """Reload the raw data from file or URL."""
1255 if self.embed:
-> 1256 super(Image,self).reload()
1257 if self.retina:
1258 self._retina_shape()
~/opt/anaconda3/lib/python3.8/site-packages/IPython/core/display.py in
reload(self)
653 """Reload the raw data from file or URL."""
654 if self.filename is not None:
--> 655 with open(self.filename, self._read_flags) as f:
656 self.data = f.read()
657 elif self.url is not None:
FileNotFoundError: [Errno 2] No such file or directory: 'cloud.png'
which evidently is not the right solution, I am a little out of my depth here and grateful for any help
That means the file does not exist in the directory it is called. You must download their 'cloud.png' and put it in the same file as the jupyter notebook file.
https://github.com/ChilesheChanda/TwitterSentimentAnalysis/blob/master/cloud.png

Python -- How to rename Musescore path in package music21?

I tried to install the Python package music21 and am having a problem running it in Windows. Basically, when I tried to run the simple command they give as an example
converter.parse("tinynotation: 3/4 c4 d8 f g16 a g f#").show()
I got an error
SubConverterException: Cannot find a path to the 'mscore' file at C:\Program Files (x86)\MuseScore 2\MuseScore.exe -- download MuseScore
The reason for this is because Musescore.exe is no longer stored in the folder "MuseScore 2" but now in a subfolder called "bin". So the path needs to be set to be "C:\Program Files (x86)\MuseScore 2\bin\MuseScore.exe" in order to access Musescore.
How do I change this?
Full Error
SubConverterException Traceback (most recent call last)
<ipython-input-8-46c66c71749d> in <module>()
----> 1 converter.parse("tinynotation: 3/4 c4 d8 f g16 a g f#").show()
C:\Users\MrNoName\Anaconda3\lib\site-packages\music21\stream\__init__.py in show(self, *args, **kwargs)
255 if self.isSorted is False and self.autoSort:
256 self.sort()
--> 257 return super(Stream, self).show(*args, **kwargs)
258
259 #---------------------------------------------------------------------------
C:\Users\MrNoName\Anaconda3\lib\site-packages\music21\base.py in show(self, fmt, app, **keywords)
2586 app=app,
2587 subformats=subformats,
-> 2588 **keywords)
2589
2590 #--------------------------------------------------------------------------
C:\Users\MrNoName\Anaconda3\lib\site-packages\music21\converter\subConverters.py in show(self, obj, fmt, app, subformats, **keywords)
312
313 if 'Opus' not in obj.classes:
--> 314 fp = helperSubConverter.write(obj, helperFormat, subformats=helperSubformats)
315
316 defaults.title = savedDefaultTitle
C:\Users\MrNoName\Anaconda3\lib\site-packages\music21\converter\subConverters.py in write(self, obj, fmt, fp, subformats, **keywords)
808
809 if subformats is not None and 'png' in subformats:
--> 810 fp = self.runThroughMusescore(fp, **keywords)
811 return fp
812
C:\Users\MrNoName\Anaconda3\lib\site-packages\music21\converter\subConverters.py in runThroughMusescore(self, fp, **keywords)
756 raise SubConverterException(
757 "Cannot find a path to the 'mscore' file at " +
--> 758 "%s -- download MuseScore" % musescorePath)
759
760 fpOut = fp[0:len(fp) - 3]
SubConverterException: Cannot find a path to the 'mscore' file at C:\Program Files (x86)\MuseScore 2\MuseScore.exe -- download MuseScore
Do this right after importing music21:
environment.set('musescoreDirectPNGPath', 'C:\\Program Files (x86)\\MuseScore 2\\bin\\MuseScore.exe')
For MuseScore 3
us = environment.UserSettings()
us['musicxmlPath'] = 'C:\\Program Files\\MuseScore 3\\bin\\MuseScore3.exe'
us['musescoreDirectPNGPath'] = 'C:\\Program Files\\MuseScore 3\\bin\\MuseScore3.exe'
us['musicxmlPath']
And if it still does not work, try opening the environment.py with sublime or else in
C:\Users\YOU\AppData\Local\Programs\Python\Python39\Lib\site-packages\music21\environment.py
then change
'%PROGRAMFILES%\MuseScore 3\MuseScore.exe'
for
'%PROGRAMFILES%\MuseScore 3\bin\MuseScore.exe'

To unzip a file

I want to unzip a file of type *.sec.gz which is a zipfile. But i'm getting badfile.....Can someone guide to resolve this.....File present in the folder is of type *.sec ........Thanks in advance
import zipfile
def unzip(path):
zfile = zipfile.ZipFile(path)
for name in zfile.namelist():
(dirname, filename) = os.path.split(name)
if filename == '':
# directory
if not os.path.exists(dirname):
os.mkdir(dirname)
else:
# file
fd = open(name, 'w')
fd.write(zfile.read(name))
fd.close()
zfile.close()
k=unzip('C://test//08October2014//DATA_INTV_NEW//Oct0814//1.sec.gz')
Output:
BadZipfile Traceback (most recent call last)
<ipython-input-7-5134b63e752e> in <module>()
27 zfile.close()
28
---> 29 k=unzip('C://test//08October2014//DATA_INTV_NEW//Oct0814//1.sec.gz')
<ipython-input-7-5134b63e752e> in unzip(path)
13
14 def unzip(path):
---> 15 zfile = zipfile.ZipFile(path)
16 for name in zfile.namelist():
17 (dirname, filename) = os.path.split(name)
C:\Python27\Lib\zipfile.pyc in __init__(self, file, mode, compression, allowZip64)
768 try:
769 if key == 'r':
--> 770 self._RealGetContents()
771 elif key == 'w':
772 # set the modified flag so central directory gets written
C:\Python27\Lib\zipfile.pyc in _RealGetContents(self)
809 raise BadZipfile("File is not a zip file")
810 if not endrec:
--> 811 raise BadZipfile, "File is not a zip file"
812 if self.debug > 1:
813 print endrec
BadZipfile: File is not a zip file
The error message is completely accurate: that is not a zip file. It is a gzip file, which is something completely different. You should use the gzip module.

Categories