1

Im trying to figure out how to edit the ending of this string:

select_file = AudioSegment.from_(select_file)

what I'm trying to make it do is for the format of the file selected (In this case, a .wav file), to change the AudioSegment to extract for each file. I.e: If i select a ".mp3" file, AudioSegment.from_wav will change to AudioSegment.from_mp3.

How can i accomplish this? It seems that any attempt to make it flexible results in this error

AttributeError: type object 'AudioSegment' has no attribute 'from_SoundFile'

Here is the full code.

for file in os.listdir("Music"):
if file.endswith(".wav") or file.endswith(".mp3"):
    print file

select_file = raw_input("Which file would you like to modify?")
if select_file in os.listdir("Music"):
    os.chdir("Music")
    print select_file
    FileEnd = select_file.split(".")
    print FileEnd[-1]
    select_file = AudioSegment.from_SoundFile[-1](select_file)   

SoundFile = { 'Name': select_file, 'format': FileEnd[-1]}

How do i make it so that the ending will change depending on the file ending? I don't need it to fit any specific type of code, so any answer is fine.

John Doe
  • 126
  • 1
  • 3
  • 15
  • This code doesn't make any sense to me. Why are you reassigning `select_file` at all? What is `from_SoundFile`? – jgritty Nov 28 '15 at 01:37
  • There's clearly some misunderstandings in the approach you have taken so it would be beneficial from the point of view of what level to pitch an answer at by knowing what the actual problem you are trying to solve with this code is. – shuttle87 Nov 28 '15 at 01:42

1 Answers1

1

First it is a good idea to use os.path.splitext to get your file extension.

filename, file_extension = os.path.splitext('example_song.mp4')

After that you have a couple of options for selecting the correct pydub function. To understand the API I'd suggest having a look at the documentation, there you see that you can use from_file with the format keyword argument:

AudioSegment.from_file(filename, format=file_extension)

Or use functions based on the extension you extracted:

if file_extension == "mp3":
    AudioSegment.from_mp3(output_filename)

You can see by looking in the pydub source that from_mp3 and similar are just aliases for from_file with certain formats.

To then export that to a file you might want to use the AudioSegment export method.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • When i try that, i get this error: IOError: [Errno 2] No such file or directory: 'boom'. Any reason that happens? – John Doe Nov 28 '15 at 15:00
  • I'm fairly sure if you spent a bit of time debugging it you would see the problem. If you have an audio file then what is missing in the file name "boom" ? – shuttle87 Nov 28 '15 at 17:22
  • I just figured it out. I had to combine your solution of AudioSegment.from_file(filename, format=file_extension) with select_file.split(".") as your os.path.splitext included the period. Thank you for your ideas. – John Doe Nov 28 '15 at 22:55
  • Glad you figured it out :) – shuttle87 Nov 28 '15 at 22:57