jiaaro / pydub

Manipulate audio with a simple and easy high level interface
http://pydub.com
MIT License
8.82k stars 1.04k forks source link

name 'pydub' is not defined #667

Open liora-symone opened 2 years ago

liora-symone commented 2 years ago

Pydub errors with Jupyter Notebook

from os import path
from pydub import AudioSegment

pydub.AudioSegment.converter = r"C:/ffmpeg/bin"

# Assign files                                                                         
src = 'C:/Users/liora/Documents/Engineering/3rd Year/Individual project (EG3005)/Music Genre Classification/archive/songs/misunderstood_lucky_daye.mp3'
dst = "C:/Users/liora/Documents/Engineering/3rd Year/Individual project (EG3005)/Music Genre Classification/archive/songs/output.wav"

# Convert wav to mp3                                                            
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")

The output I believe I should get is:

<_io.Bufferedrandom name='dst.wav'>

The error displayed:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-7a7c916717bd> in <module>
      2 from pydub import AudioSegment
      3 
----> 4 pydub.AudioSegment.converter = r"C:/ffmpeg/bin"
      5 
      6 # Assign files

NameError: name 'pydub' is not defined

My System configuration:

Is there an audio file you can include to help us reproduce?

I'm not sure how to attach it, but it's an audio file that's roughly 3 minutes long.

Nneji123 commented 2 years ago

Have you tried this?

AudioSegment.converter = r"C:/ffmpeg/bin"
Kagirim commented 2 years ago

Hello. I believe this is because you imported the AudioSegment object from pydub so you should reference it directly ie AudioSegment.converter = r"C:/ffmpeg/bin" If you wanted to reference it as pydub.AudioSegment.converter = r"C:/ffmpeg/bin" change import statement to import pydub

liora-symone commented 2 years ago

Thank you so much for the advice, the error is gone! But now I get this:

---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
<ipython-input-59-af4d73abe6b6> in <module>
     11 
     12 # Convert wav to mp3
---> 13 sound = AudioSegment.from_mp3(src)
     14 sound.export(dst, format="wav")

~\anaconda3\envs\tensorflow\lib\site-packages\pydub\audio_segment.py in from_mp3(cls, file, parameters)
    794     @classmethod
    795     def from_mp3(cls, file, parameters=None):
--> 796         return cls.from_file(file, 'mp3', parameters=parameters)
    797 
    798     @classmethod

~\anaconda3\envs\tensorflow\lib\site-packages\pydub\audio_segment.py in from_file(cls, file, format, codec, parameters, start_second, duration, **kwargs)
    764         log_conversion(conversion_command)
    765 
--> 766         p = subprocess.Popen(conversion_command, stdin=stdin_parameter,
    767                              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    768         p_out, p_err = p.communicate(input=stdin_data)

~\anaconda3\envs\tensorflow\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    856                             encoding=encoding, errors=errors)
    857 
--> 858             self._execute_child(args, executable, preexec_fn, close_fds,
    859                                 pass_fds, cwd, env,
    860                                 startupinfo, creationflags, shell,

~\anaconda3\envs\tensorflow\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
   1309             # Start the process
   1310             try:
-> 1311                 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
   1312                                          # no special security
   1313                                          None, None,

PermissionError: [WinError 5] Access is denied

I've tried to change the permission on the folder but it's not working, and I have conflicts while trying to update python in anaconda.

Kagirim commented 2 years ago

Hello. Hope youre doing great! The win error 5 youre getting seems to be windows folder permissions. I'd try to run the python script under admin privileges or change the path of the source file to a non "C" one because that's where most of the permission issues arise. You could also try to change the permission settings on the path folder. windows defender or antivirus might scream at you so I usually avoid placing external files in "C". look at this as well How to fix - Error 5: Access is Denied in Windows 10 - Microsoft Community https://answers.microsoft.com/en-us/windows/forum/all/how-to-fix-error-5-access-is-denied-in-windows-10/8a4069ea-4b0e-4517-9d7a-62c44b4d6e7d happy if I was any help Thanks.

On Tue, Jun 7, 2022 at 10:50 AM psylockes @.***> wrote:

Thank you so much for the advice, the error is gone! But now I get this:


PermissionError Traceback (most recent call last)

in 11 12 # Convert wav to mp3 ---> 13 sound = AudioSegment.from_mp3(src) 14 sound.export(dst, format="wav") ~\anaconda3\envs\tensorflow\lib\site-packages\pydub\audio_segment.py in from_mp3(cls, file, parameters) 794 @classmethod 795 def from_mp3(cls, file, parameters=None): --> 796 return cls.from_file(file, 'mp3', parameters=parameters) 797 798 @classmethod ~\anaconda3\envs\tensorflow\lib\site-packages\pydub\audio_segment.py in from_file(cls, file, format, codec, parameters, start_second, duration, **kwargs) 764 log_conversion(conversion_command) 765 --> 766 p = subprocess.Popen(conversion_command, stdin=stdin_parameter, 767 stdout=subprocess.PIPE, stderr=subprocess.PIPE) 768 p_out, p_err = p.communicate(input=stdin_data) ~\anaconda3\envs\tensorflow\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text) 856 encoding=encoding, errors=errors) 857 --> 858 self._execute_child(args, executable, preexec_fn, close_fds, 859 pass_fds, cwd, env, 860 startupinfo, creationflags, shell, ~\anaconda3\envs\tensorflow\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session) 1309 # Start the process 1310 try: -> 1311 hp, ht, pid, tid = _winapi.CreateProcess(executable, args, 1312 # no special security 1313 None, None, PermissionError: [WinError 5] Access is denied I've tried to change the permission on the folder but it's not working, and I have conflicts while trying to update python in anaconda. — Reply to this email directly, view it on GitHub , or unsubscribe . You are receiving this because you commented.Message ID: ***@***.***>
liora-symone commented 2 years ago

Thank you, I'll try this and get back to you!