I'm trying to convert some audio files using a python script. The source folder contains both .wav and .aiff audio files, with different bitrates and sampling rates.
INPUT Files example
I need all the audio files to be converted to mono 16bit 44.1khz .wav
OUTPUT Files example
At the moment I'm succesfully using pysoundfile library to open and write/convert .wav files to PCM_16 type.
What I don't know how to do, is converting .aiff files to .wav ones.
I know Python handles .aiff files with the aifc library, but it doesn't seems to include any kind of conversion functionality.
How can I do that?
Disclaimer: Probably this is not the answer you're looking for, because it is not "Python-only"; however, when I tried to work with media files in a large Python script I found this method easier than searching and reading documentation of a bunch of libraries for each media format I needed.
Install a media converter (f.e., I use FFmpeg), whose commands you can execute from Windows command line or Linux terminal. Then you can call these commands from a Python script:
from subprocess import call
call('ffmpeg -i D:/Audio/IN/sample.aiff -acodec pcm_s16le -ac 1 -ar 44100 D:/Audio/OUT/sample.wav')
(Here '-ac 1' means 1 audio track, that is, we convert to mono.) You can also extract audio from any video format in a similar way.
This is a non-issue. The soundfile module you are already using does support AIFF files out-of-the-box. Just specify the file name and you are done!
You can use the function soundfile.available_formats() to get a list of supported formats.
The soundfile module uses the libsndfile library under the hood. There is a list of supported formats on their homepage (which contains AIFF).
Like #Wolfram mentioned, you can of course also use FFmpeg instead.
Another great external tool for audio conversion is SoX.
Related
I am looking for a package where I can use to convert the audio file from mp4 to wav simultaneously while also cropping the file using python.
I have seen some posts about using ffmpeg to do the conversion but I dont seem to be able to run ffmpeg within python, and when converted to wav the files become around 10 times bigger which is kind of a storage issue.
Does anyone have any suggestions except for pipelining it?
How can I use Python to convert a qcow2 image file into a raw image file?
I know of qemu-img, but I'm curious about any Python libraries that might allow me to avoid asking my users to install that tool. It's not packaged with a default Fedora install, and that's what I'm developing for. If there are no other options however, I'll use qemu-img.
It seems that qemu-img is a necessity for converting qcow2 image files to raw images. I did not find a solution that avoided calling on this tool. This isn't a big issue though, because qemu-img is widely available in distros' repositories, and is sometimes packaged with distros. In order to make use of this tool in Python, simply ensure that it's installed to the system and then call it programmatically via the subprocess module, like so:
import subprocess
# Assuming file_path is the path to a local qcow2 file
if file_path.endswith('.qcow2'):
raw_file_path = file_path[:5] + '.raw'
subprocess.call(['qemu-img', 'convert', file_path, raw_file_path])
I'm looking for a Python library that can combine images into a video.
A library that just allows you to create an empty video and feed images into it as frames is ideal.
Preferably with support for MPEG compression of the video file as well.
If you run linux then you can use ffmpeg to do this from the command line there is a python wrapper called pyFFmpeg that you can use - there is also pymedia but it doesn't look to be maintained.
BTW there are a number of projects that provide builds of ffmpeg for windows.
gstreamer is the tool you are looking for. you'll probably need an appsrc or something like that.
Wondering which Python module to use to extract meta info (including container format, codec format, resolution, frame rate, length) from video/audio files?
Would make for an interesting module. Here's an SO question for MP3's: Accessing mp3 Meta-Data with Python
Sourceforge python package that does some images and audio formats: http://sourceforge.net/projects/mmpython/
Not sure if it covers all the formats you're looking for.
Does anybody have experience with converting mp4 files to .wav or mp3 files? I am able to do this in Linux (bash), but I try to do everything in Python that I do in other languages, call me an enthusiast. I have been looking over the Pymedia library, but have not made progress as of yet.
You can use Python bindings for GStreamer, and create a pipeline to do the conversion:
More info here:
http://pygstdocs.berlios.de/pygst-tutorial/pipeline.html
Example of pipeline in another SO question:
converting wav to mp3 (and vice versa) using GStreamer
You might find the python audio tools of some use. They are designed to work from command line, but being python code you can simply import the modules and integrate it in another program. This is the API documentation. From the "About" page:
Python Audio Tools are a collection of audio handling programs which work from the command line. These include programs for CD extraction, track conversion from one audio format to another, track renaming and retagging, track identification, CD burning from tracks, and more. Supports internationalized track filenames and metadata using Unicode. Works with high-definition, multi-channel audio as well as CD-quality. Track conversion uses multiple CPUs or CPU cores if available to greatly speed the transcoding process. Track metadata can be retrieved from FreeDB, MusicBrainz or compatible servers.