Export (but not save to) an audio file in PyDub? - python

The PyDub library, for me, is pretty much ideal for converting audio formats. I recently used it to write a command line audio converter to convert about 200 audio files, and it saved me having to buy or look for an audio converter that would allow me to queue up songs and other audio files for conversion. But I quickly noticed that it replaced my audio files. Now, for me, this was ideal. This was great. But what if I didn't want PyDub to replace the audio files, but rather duplicate it but in a different format? I could just copy the files into the directory and convert them, but is there no way to do this from within PyDub? I looked into it and I couldn't find a way to do this, nor could I find a question on this, so maybe this isn't a very common thing to do.
Thanks!

When you export an audio segment, you can always specify a new name for the file (or use the same name but in a different folder)
from pydub import AudioSegment
song = AudioSegment.from_file("/path/to/file.mp3", format="mp3")
song.export("/path/to/new/filename.mp4", format="mp4")

Hope this helps:
myaudio = AudioSegment.from_mp3("XXXXX/y.mp3")
chunk_length_ms = 1000000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) # Make chunks of one sec
chunks.export('path where file needs to be exported' + chunks, format='mp3')

Related

Get duration from multiple video files?

I want to extract video duration metadata from every video file in a specified directory and then view the total duration.
I need to extract the data from as much as thousands of videos overall. In Windows I can view the total duration for many files manually when selecting them in the explorer and going into details. For 1500 mp4 files it takes about 20 seconds to make the calculations and view the total time. It's relatively much faster then what I'm currently getting when iterating with FFprobe.
How fast I am currently getting the result with FFprobe.
for filename in dirFiles:
print(subprocess.check_output(['ffprobe', '-i', filename, '-show_entries','format=duration', '-sexagesimal' ,'-v', 'quiet', '-of', 'csv=%s' % ("p=0")]))
What is the faster way to do this?
I solved the problem with mutagen module as it handles files quite fast.
mp4 = mutagen.mp4.MP4(filename)
duration+=mp4.info.length
There is no "best" way, just a best way for your use case. os.stat doesn't have it because video duration is not part of any posix file system, (files systems don't care about the contents of a file, Whats is the duration of a text file? what is the resolution of an executable?) If you don't like ffprobe, try mediainfo, or mp4box, or any other tool that can read media files.

Converting .ul files

I recently copied a bunch of audio files, which are feedback left during a phone call.
The vast majority of them are mp3, but a small percentage are files ending in a .ul extension, which I believe is ULAW.
I have tried to play them in Audacity and VLC, but get garbled sounds. I suspect they are corrupted, but I'd like to confirm that by attempting to convert them to another audio format.
Would anyone be able to recommend a library to do that?
I know Python has the audioop module but I do not know enough to start messing with the audio data.

How to find duration of online audio file without downloading it using python?

I have url of a wav format audio file which is basically an audio of a call . I want to find the duration of the wav file, which means the duration of the call. I do not want to download the wav file, as I have to repeat this set for a large number of such records. Is there any way to do this in python ?
A little more information about where the audio file would be helpful.
Such as the URL or the location of the file. You might be able to use beautiful soup to scrape for the length if it's mentioned on a webpage, otherwise, you might be able to use some sort of an API call.
https://github.com/quodlibet/mutagen would helpful to look at
You will need to download some of the WAV files to be able to read the header and parse it with e.g. the builtin wave module.
Alternately, if all of the files are uncompressed PCM, and have the same format, you can just look at the file size (which you can get with a HTTP HEAD request) and guess the approximate duration.

How to play Wav sound samples from memory

For my final year project in college I am working with Wav Files and Python and messing around with them. I would love to be able to play the sound samples from memory rather than write the sound samples out in to a WAV file before I can hear them.
I have been looking for weeks online and have found PyMedia, PySound, PyGame etc and none of them seem to work for me. Every single package gives me errors.
Are there other libraries I am missing that would help me do this ? Or am I just being stupid and can't get the other packages to work.
Exactly what I want to do is along the lines of this:
#open file and get parameters
wavfile = Wave.open("file.wav", "r")
params = wfile.getparams()
nframes = params[3]
#get sound samples in a list
samples = []
for i in range(nframes):
samples.append(wfile.readframes(1))
playsound(samples)
changedSamples = makeChangeTo(samples)
playsound(changedSamples)
And I would like to be able to have this in a loop so I can edit and hear the edits while the program is still runnning without having to write the samples to a wav file before being able to hear it as that takes too long.
Any suggestions ? Cheers !
You should clearly separate those two concerns:
Reading/writing WAV files (or other audio files)
Playing/recording sounds
There are several questions and answers to both topics here on SO.
This is my personal (and of course biased) recommendation:
You should use NumPy to manipulate sounds, it's much easier than handling plain Python buffers.
If for some reason you cannot use NumPy, you can still do all this, but it will be a bit more work.
For reading/writing sound files, I recommend the soundfile module (full disclosure: I'm a co-author).
For playing/recording sounds, I recommend the sounddevice module (full disclosure: I'm its main author).
When using those modules, your example would probably become something like this:
import soundfile as sf
import sounddevice as sd
samples, samplerate = sf.read('file.wav')
sd.play(samples, samplerate)
sd.wait()
changed_samples = make_change_to(samples)
sd.play(changed_samples, samplerate)
sd.wait()
If you work in an interactive Python prompt, you probably don't need the sd.wait() calls, you can just wait until the playback has finished. Or, if you get bored of listening to it, you can use:
sd.stop()
If you know that you will use the same sampling rate for some time, you can set it as default:
sd.default.samplerate = 48000
After that, you can drop the samplerate argument when using play():
sd.play(samples)
If you want to store the changed sound to a file, you can use something like this:
sf.write('changed_file.wav', changed_samples, samplerate)
Further reading:
different options for reading/writing audio files
different options for playback/recording
a very basic tutorial about handling audio signals

How do I mix audio files using python?

I would like to do basic audio mixing in python.
To give an example: I would like to take two mp3 files and add them together and return one mp3 file. Another example: I would like to take the first ten seconds of one mp3 file and add it to the beginning of another mp3 file.
What is the best way to accomplish these tasks? I would like to use built in python functions like audioop but can not find any good tutorials or sample code out there for using the built in functions.
I am going through the docs but I am pretty confused and cannot figure out how to do things like this. I am not even sure that the python libraries like mp3's. Most of the stuff I have looked at seem to refer to WAV files. So, if this is the case, I guess a follow up question would be is there an easy way to convert an mp3 to a WAV for manipulation and back again?
You can do this pretty easily using pydub:
from pydub import AudioSegment
sound1 = AudioSegment.from_mp3("/path/to/file1.mp3")
sound2 = AudioSegment.from_mp3("/path/to/file1.mp3")
# mix sound2 with sound1, starting at 5000ms into sound1)
output = sound1.overlay(sound2, position=5000)
# save the result
output.export("mixed_sounds.mp3", format="mp3")
You could check out some of the code in the python audiotools project. It is a collection of command-line utilities which make use of a common python package. There is a utility included with audiotools (trackcat) which can con*cat*enate two or more audio tracks; another (tracksplit) can split an audio track (using a .cue file). These, as well as the numerous other included utilities, can work with audio files of various encodings, including mp3.
The way I've done this in the past is just use subprocess. and call sox.
E.g. subprocess.call(["sox", "in.1.mp3", "in.2.mp3", "out.mp3"])

Categories