0

While trying to use an mp3 file in a python environment I get this error, my file directories are all properly spelt.

Note: the directories are formatted so that within a project folder there is the main code, and 2 sub-folders one for the non-altered mp3 files ("targets"), and one for the altered mp3 files ("altered_targets").

I have already tried to switch AudioSegment.from_file("filename","mp3") with no luck.

from pydub.playback import play
from pydub import AudioSegment
import sys
import os

sys.path.append('/usr/local/lib/python3.7/dist-packages/ffmpeg')

def adjust_volume(file_name, adjustment):

   song = AudioSegment.from_mp3(file_name)

   new_song = song + adjustment

   new_title = "./alter_targets/%s" % (file_name)

   new_song.export(new_title, format='mp3')

for target in os.listdir("./targets"):
   adjust_volume("./targets/"+target, 10)

This code snippet should slightly increase the volume of the mp3 file within "targets", then save the new mp3 file into the "altered_targets" file.

Instead I get "OSError: [Errno 2] No such file or directory"

Traceback (most recent call last):
  File "volume_alter.py", line 19, in <module>
    adjust_volume("./targets/"+target, 10)
  File "volume_alter.py", line 10, in adjust_volume
    song = AudioSegment.from_mp3(file_name)
  File "/usr/local/lib/python2.7/dist-packages/pydub/audio_segment.py", line 716, in from_mp3
    return cls.from_file(file, 'mp3', parameters=parameters)
  File "/usr/local/lib/python2.7/dist-packages/pydub/audio_segment.py", line 665, in from_file
    info = mediainfo_json(orig_file)
  File "/usr/local/lib/python2.7/dist-packages/pydub/utils.py", line 263, in mediainfo_json
    res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
Kimchoo
  • 261
  • 1
  • 3
  • 5
  • You're using `./`, meaning it'll only look for files in the current directory – clubby789 Aug 05 '19 at 14:30
  • @JammyDodger, can I not use "./targets/file.mp3" ? – Kimchoo Aug 05 '19 at 14:37
  • I think I misunderstood the code. Can you get it to print out `"./targets/"+target` before it runs `adjust_volume()` to see if it's looking for a correct path? – clubby789 Aug 05 '19 at 14:39
  • Also which line is the error on? – clubby789 Aug 05 '19 at 14:39
  • @JammyDodger "song = AudioSegment.from_mp3(file_name)" – Kimchoo Aug 05 '19 at 14:41
  • Check to make sure `from_mp3` allows passing in a subfolder – clubby789 Aug 05 '19 at 14:50
  • @JammyDodger, I changed my code slightly so that from_mp3 does not need to navigate to a sub-folder but it still gave me the same error. – Kimchoo Aug 05 '19 at 14:54
  • 1
    I would suggest to use `os.path` to manipulate your path variable (e.g. `new_title = os.path.join([os.path.dirname(__file__), 'alter_targets', file_name])`) – user7440787 Aug 05 '19 at 14:55
  • Sometimes, depending on how you invoke a script, the working directory of the process is not where you would expect, so relative paths do not resolve as you would expect. You can see what the working directory of the process is by calling `os.getcwd()`. If it is not the parent directory of `targets`, then you know why you're seeing this error. – Alex Aug 05 '19 at 15:28
  • @Alex, good suggestion but upon checking it is not the source of my issue. – Kimchoo Aug 05 '19 at 16:28
  • Can you post the full traceback you're receiving? – Alex Aug 05 '19 at 17:38
  • @Alex, I updated the question to include the traceback – Kimchoo Aug 05 '19 at 18:10

0 Answers0