1

I'm attempting to get pydub to mix 2 audio wav files into a single wav file. Here is my current code:

from pydub import AudioSegment

sound1 = AudioSegment.from_wav("Test_1.wav")
sound2 = AudioSegment.from_wav("Test_2.wav")

# mix sound2 with sound1, starting at 5000ms into sound1)
output = sound1.overlay(sound2, position=5000)

# save the result
output.export("mixed.wav", format="wav")

here is the exact error log:

Traceback (most recent call last):
  File "heterodyning.py", line 10, in <module>
    output.export("mixed.wav", format = "wav")
  File "build/bdist.linux-armv6l/egg/pydub/audio_segment.py", line 234, in export
  File "/usr/lib/python2.7/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

However, for whatever reason, whenever i try to run this, it raises a raise_child exception, and tells me that there is no such file or directory, referring to the last line where I tell it to export. I have no idea why. Can anyone tell me why this is happening?

Philip R.
  • 345
  • 3
  • 4
  • 12

1 Answers1

0

What does your current working directory look like? If there is no "mixed.wav" file in there, then output.export("mixed.wav", format="wav") will fail because it doesn't know where to put the overlayed sound. You can open a file in python via:

mixed_file = open("mixed.wav", "wb")

Do this before exporting to the file.

You should end up with something like this:

mixed_file = open("mixed.wav", "wb")
output.export(mixed_file, format="wav")
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
The2ndSon
  • 307
  • 2
  • 7
  • i can confirm that currently, my code will produce the mixed.wav file, but it will not actually put the mixed audio in it. adding your bit of code to open the file here did not work, it gave me the exact same error. – Philip R. Jul 25 '14 at 20:30
  • Okay. After taking a look at the 'wave' python library it may be useful to import it https://docs.python.org/2/library/wave.html. You can then do something like `mixed_file = wave.open("mixed.wav", "w")` and `mixed_file.writeframes(output)` – The2ndSon Jul 25 '14 at 20:37
  • @PhilipR. you should open it as a binary file `mixed_file = open("mixed.wav", "wb")`, and you need to pass the file in to the export method: `output.export(mixed_file, format="wav")` – Jiaaro Jul 25 '14 at 21:00
  • @PhilipR. I suspect the file that can't be found is not "mixed.wav" though. since the error is bubbling up from subprocess, that probably means it's trying to call `ffmpeg`, and *that* is the file that can't be found. ffmpeg is *not* supposed to be used (in fact no subprocess call should even be made) when you are exporting a wav. All other formats *do* use ffmpeg though. – Jiaaro Jul 25 '14 at 21:05
  • @Jiaaro If thats the case, though, then how do i fix it? how do i get it to not use ffmpeg? – Philip R. Jul 25 '14 at 22:01
  • @PhilipR. when `format` is `"wav"` ffmpeg is not used. See [the export method](https://github.com/jiaaro/pydub/blob/master/pydub/audio_segment.py#L371) for more details – Jiaaro Jul 28 '14 at 15:37