Why AudioSegment doesn't read 'mp3'? - python

I tried to read file that I give with absolute path.
When I run my code first that I see is this message:
D:\prog\datascience\anaconda\lib\site-packages\pydub\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
I tried this:
PATH_TO_FFMPEG = 'D:\\prog\\ffmpeg-win-2.2.2\\ffmpeg.exe'
pydub.AudioSegment.converter = r'D:\\prog\\ffmpeg-win-2.2.2\\ffmpeg.exe'
And I separately installed ffmpeg with pip. But it didn't help.
When I try this:
raw_sound = pydub.AudioSegment.from_mp3(file=track_path)
where track_path is correct absolute path generated automatically.
So I got this this error:
Traceback (most recent call last):
File "D:\prog\PyCharm Community Edition 2020.2.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1448, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "D:\prog\PyCharm Community Edition 2020.2.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/testtask2/test_task/testtask/get_mffc.py", line 165, in <module>
slice_all_in_a_dir('May 27 2020 LNC/Hydrophone 1/raw_records')
File "D:/testtask2/test_task/testtask/get_mffc.py", line 70, in slice_all_in_a_dir
slice_samples(track_path= [file],
File "D:/testtask2/test_task/testtask/get_mffc.py", line 48, in slice_samples
raw_sound = pydub.AudioSegment.from_mp3(file=track_path)
File "D:\prog\datascience\anaconda\lib\site-packages\pydub\audio_segment.py", line 738, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)
File "D:\prog\datascience\anaconda\lib\site-packages\pydub\audio_segment.py", line 680, in from_file
stdin_data = file.read()
AttributeError: 'list' object has no attribute 'read'
python-BaseException
Full code when I use it:
def slice_samples(track_path: list, save_path: str,
sample_folder_name: str, interval: float, given_format, name: str = "part", export_format = 'wav'):
"""
This metod slice given track to parts.
:param track_path: str, a path to the track you want to slice
:param save_path: str, a path to folder, where you want save sliced tracks
:param sample_folder_name: str, you don't need to create a folder for sliced tracks,
you can just write the name of the folder in this argument where you want to save tracks
:param interval: float, measure in seconds, the length of sliced tracks
:param name: str, name of sliced tacks
:param given_format: str, I strongly recommend use .wav format initially, when you record sounds
:return: folder with sliced tracks
"""
# it cuts a file in mp3 or wav formats (wav recommended)
interval_secs = interval * 10 ** 3
raw_sound = None
if given_format == "WAV":
raw_sound = pydub.AudioSegment.from_wav(file=track_path)
elif given_format == "MP3":
raw_sound = pydub.AudioSegment.from_mp3(file=track_path)
else:
raise Exception("It's temporarily unsupported given_format: " + given_format)
start = 0
end = interval_secs
i = 0
while end < len(raw_sound):
save_to = save_path + sample_folder_name + "/" + name + str(i)
part = raw_sound[start:end]
part.export(save_to, format=export_format)
i += 1
start += interval_secs
end += interval_secs
return save_path + sample_folder_name
def slice_all_in_a_dir(tracks_folder: str):
files = os.listdir(tracks_folder)
for file in files:
folder_name = file.split('.')
f_name = folder_name[0]
file = tracks_folder+'/'+file
file = os.path.abspath(file)
slice_samples(track_path= [file],
save_path= PATH_FOR_SLICED,
sample_folder_name= f_name,
interval=5,
given_format=folder_name[1])
if __name__ == "__main__":
slice_all_in_a_dir('May 27 2020 LNC/Hydrophone 1/raw_records')

can you please share the code which you are using , It will be helpful to find the exact issue.
AttributeError: 'list' object has no attribute 'read'
Also the error suggests that file is being treated as a list and not as file in the below code
stdin_data = file.read()
Hope I have given a solution.

Related

Errors with Python and eyed3

import os
import eyed3
def files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
def title_alteration(music_artist_string):
music_artist_string = music_artist_string.replace(';', ' feat. ', 1)
semicolon_count = music_artist_string.count(';')
music_artist_string = music_artist_string.replace(';', ', ', semicolon_count-1)
music_artist_string = music_artist_string.replace(';', ' & ')
return music_artist_string
def main():
audio_files = eyed3.load(files('D:\\iTunes Music\\iTunes\\iTunes Media\\Music'))
title_alteration(audio_files.tag.artist)
if __name__ == '__main__':
main()
Can I get some help debugging this, I got it down to three distinct functions with some help from my last post, now I just need to know why is this getting errors when I attempt to run it on this directory on my pc.
I'm getting these errors (TLDR; It doesnt like Line 20 [the audio_files line]):
Traceback (most recent call last):
File "D:/Pycharm Projects/Music Alterations v2.py", line 25, in <module>
main()
File "D:/Pycharm Projects/Music Alterations v2.py", line 20, in main
audio_files = eyed3.load(files('D:\\iTunes Music\\iTunes\\iTunes Media\\Music'))
File "C:\Users\cLappy\AppData\Local\Programs\Python\Python38\lib\site-packages\eyed3\core.py", line 74, in load
path = pathlib.Path(path)
File "C:\Users\cLappy\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 1038, in __new__
self = cls._from_parts(args, init=False)
File "C:\Users\cLappy\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 679, in _from_parts
drv, root, parts = self._parse_args(args)
File "C:\Users\cLappy\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 663, in _parse_args
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not generator
Process finished with exit code 1
Your files function is a generator, to be used like an iterator. The eyed3.load function doesn't want a generator or an iterator. It wants a pathname or something like one. Passing a generator where a pathname is expected does not magically cause iteration over all the values the generator would generate. It would work better just to make a list of all the pathnames of interest and then iterate over that list, calling eyed3.load for each pathname.

ffmpeg output error of YouTube videos in Python?

I'm not trying to download the whole video, just 5 seconds of a video. I tried following this guide. However, this is for in line command and I'm trying to do this with python code. So far I've managed to extract all the required information using youtube-dl. The problem I'm having now is using ffmpeg to output the video with the given parameters, but it's saying it cannot find the file specified.
This is what I have so far:
import youtube_dl
import datetime
import ffmpeg
def getInfo():
link='https://youtu.be/nda20uSjQUI?t=11981'
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'})
#This exctracts all the information of the link
with ydl:
result = ydl.extract_info(
link,
download=False
)
#Grabs the duration of the video
duration_info = result.get('duration')
#Grabs the url to the actual video file
url = str(result).split("'url':",1)[1].split(', ')[0]
#YouTube(link).streams.first().download('D:/Downloads')
#Determines the start time based on how the link is formatted
#They can be in three possible formats
if 't=' in link:
if link.endswith('s'):
time_seconds = link.split('t=', 1)[1].strip('s')
else:
time_seconds = link.split('t=', 1)[1]
else:
time_seconds = '0'
#Converts the time into an HH:MM:ss format
#We only want 5 seconds of video, which is what timestamp_end does
timestamp_start = str(datetime.timedelta(seconds=int(time_seconds)))
timestamp_end = str(datetime.timedelta(seconds=int(time_seconds)+5))
video_length = str(datetime.timedelta(seconds=int(duration_info)))
#If the segment we want is less than 5 seconds, such as towards the end of the video,
#then the end will be the end of the actual video
if timestamp_end > video_length:
timestamp_end = video_length
#Returns a list of [url, start, end]
return url ,timestamp_start,timestamp_end
def download_video(result):
#Checks to see if there's a valid argument
if result is None:
return 'Error: no url found'
else:
file_input = ffmpeg.input(result[0])
out = ffmpeg.concat(file_input.trim(start_frame=result[1],end_frame=result[2])).output('test1.mp4').run()
return out
download_video(result=getInfo())
And this is the error I'm getting:
Traceback (most recent call last):
File "C:/Users/Anonymous/PycharmProjects/untitled/test.py", line 53, in <module>
download_video(result=getInfo())
File "C:/Users/Anonymous/PycharmProjects/untitled/test.py", line 50, in download_video
out = ffmpeg.concat(file_input.trim(start_frame=result[1],end_frame=result[2])).output('test1.mp4').run()
File "C:\Users\Anonymous\PycharmProjects\untitled3\venv\lib\site-packages\ffmpeg\_run.py", line 320, in run
overwrite_output=overwrite_output,
File "C:\Users\Anonymous\PycharmProjects\untitled3\venv\lib\site-packages\ffmpeg\_run.py", line 285, in run_async
args, stdin=stdin_stream, stdout=stdout_stream, stderr=stderr_stream
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Process finished with exit code 1

Python Pillow - ValueError: Decompressed Data Too Large

I use the Pillow lib to create thumbnails. I have to create a lot of them, actually more than 10.000
The program works fine, but after processing round about 1.500, I get the following error:
Traceback (most recent call last):
File "thumb.py", line 15, in <module>
im = Image.open('/Users/Marcel/images/07032017/' + infile)
File "/Users/Marcel/product-/PIL/Image.py", line 2339, in open
im = _open_core(fp, filename, prefix)
File "/Users/Marcel/product-/PIL/Image.py", line 2329, in _open_core
im = factory(fp, filename)
File "/Users/Marcel/product-/PIL/ImageFile.py", line 97, in __init__
self._open()
File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 538, in _open
s = self.png.call(cid, pos, length)
File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 136, in call
return getattr(self, "chunk_" + cid.decode('ascii'))(pos, length)
File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 319, in chunk_iCCP
icc_profile = _safe_zlib_decompress(s[i+2:])
File "/Users/Marcel/product-/PIL/PngImagePlugin.py", line 90, in _safe_zlib_decompress
raise ValueError("Decompressed Data Too Large")
ValueError: Decompressed Data Too Large
My program is very straight forward:
import os, sys
import PIL
from PIL import Image
size = 235, 210
reviewedProductsList = open('products.txt', 'r')
reviewedProducts = reviewedProductsList.readlines()
t = map(lambda s: s.strip(), reviewedProducts)
print "Thumbs to create: '%s'" % len(reviewedProducts)
for infile in t:
outfile = infile
try:
im = Image.open('/Users/Marcel/images/07032017/' + infile)
im.thumbnail(size, Image.ANTIALIAS)
print "thumb created"
im.save('/Users/Marcel/product-/thumbs/' + outfile, "JPEG")
except IOError, e:
print "cannot create thumbnail for '%s'" % infile
print "error: '%s'" % e
I am performing this operation locally on my MacBook Pro.
This is to protect against a potential DoS attack on servers running Pillow caused by decompression bombs. It occurs when a decompressed image is found to have too large metadata. See http://pillow.readthedocs.io/en/4.0.x/handbook/image-file-formats.html?highlight=decompression#png
Here's the CVE report: https:// www.cvedetails.com/cve/CVE-2014-9601/
From a recent issue:
If you set ImageFile.LOAD_TRUNCATED_IMAGES to true, it will suppress
the error (but still not read the large metadata). Alternately, you can
change set the values here: https://github.com/python-pillow/Pillow/ blob/master/PIL/PngImagePlugin.py#L74
https://github.com/python-pillow/Pillow/issues/2445
following code should help you in setting what accepted answer says.
from PIL import PngImagePlugin
LARGE_ENOUGH_NUMBER = 100
PngImagePlugin.MAX_TEXT_CHUNK = LARGE_ENOUGH_NUMBER * (1024**2)
It's not documented how to set this value. I hope people find this useful.

Get name attribute of IO_Bufferedreader

What I am wanting to do is use the name of the current file I have that is from a generator and use the first section of the name + append .csv
The buffered stream looks like this
<_io.BufferedReader name='data/20160107W FM0.xml'>
I am having an issue with this code:
for file_to_read in roots:
print(file_to_read)
base = os.path.basename(file_to_read)
print(base)
name_to_write = os.path.splitext(file_to_read)[0]
outname = str(name_to_write[0]) + ".csv"
outdir = "output"
with open(os.path.join(outdir, outname), 'w', newline='') as csvf:
I receive this error which I believe means I am trying to split the stream rather than the name attribute of the buffered stream. Which leads me to this error.
$ python race.py data/ -e .xml
<_io.BufferedReader name='data/20160107W FM0.xml'>
Traceback (most recent call last):
File "race.py", line 106, in <module>
data_attr(rootObs)
File "race.py", line 40, in data_attr
base = os.path.basename(file_to_read)
File "C:\Users\Sayth\Anaconda3\lib\ntpath.py", line 232, in basename
return split(p)[1]
File "C:\Users\Sayth\Anaconda3\lib\ntpath.py", line 204, in split
d, p = splitdrive(p)
File "C:\Users\Sayth\Anaconda3\lib\ntpath.py", line 139, in splitdrive
if len(p) >= 2:
TypeError: object of type '_io.BufferedReader' has no len()
My expected output is:
20160107W FM0.csv
for a file you are reading/writing this works:
filepath = '../data/test.txt'
with open(filepath, 'w') as file:
print(file) # -> <_io.TextIOWrapper name='../data/test.txt' mode='w' encoding='UTF-8'>
print(file.name) # -> ../data/test.txt
but the type here is <_io.TextIOWrapper name='../data/test.txt' mode='w' encoding='UTF-8'> so i am not entirely sure how you open your file or get a _io.BufferedReader.
i assume they are both derived from io.FileIO and should therefore have a .name attribute.
thanks to Ashwini Chaudhary's comment, i can recreate your exact situation:
from io import BufferedReader
filepath = '../data/test.txt'
with BufferedReader(open(filepath, 'r')) as file:
print(file) # -> <_io.BufferedReader name='../data/test.txt'>
print(file.name) # -> ../data/test.txt

TypeError: Can't convert '_io.TextIOWrapper' object to str implicitly

I would like to save a pandas.DataFrame object into a .csv file (using DataFrame.to_csv()). To make it simple, here is the situation; my project 'Algos' contains the folder 'Plip' with a .txt file called 'plop.txt':
1,2
3,4
5,6
My script is here:
import numpy as np
import pandas as pa
# opening the file
dossier = "Plip"
fichier = "plop.txt"
fichier = open(dossier + "/" + fichier)
data = fichier.readlines()
# creating the data frame
Pair = []
Impair = []
for m in data:
impair = int(m[0:1])
pair = int(m[2:3])
Impair.append(impair)
Pair.append(pair)
M = np.array([Impair, Pair]).transpose()
Table = pa.DataFrame(M, columns = ["Impair", "Pair"])
#creating the .csv file
Table.to_csv(dossier + fichier + ".csv")
Table has been correcty created but the script returns:
runfile('C:/Users/******/Documents/PYTHON/Algos/truc.py', wdir='C:/Users/******/Documents/PYTHON/Algos')
Traceback (most recent call last):
File "<ipython-input-110-3c7306b8d61d>", line 1, in <module>
runfile('C:/Users/******/Documents/PYTHON/Algos/truc.py', wdir='C:/Users/******/Documents/PYTHON/Algos')
File "C:\Users\******\Documents\Gratuiciels\WINPYTHON.3355\python-3.3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
execfile(filename, namespace)
File "C:\Users\******\Documents\Gratuiciels\WINPYTHON.3355\python-3.3.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 80, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/******/Documents/PYTHON/Algos/truc.py", line 30, in <module>
Table.to_csv(dossier + fichier + ".csv")
TypeError: Can't convert '_io.TextIOWrapper' object to str implicitly
I'm quite new on Python so could you be as precise as possible on your answer please ?
Thank you in advance
Seems like you reassigned fichier to a file object on L7, use a new variable instead!

Categories