Convert mp4 to .wav or mp3 with python - python

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.

Related

Converting .aiff audio file to .wav

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.

Playing sound files in python

What is a good package for playing MP3 files in python? It would need to be something that is maintained and is cross platform (Win, Mac, Linux) and something with a simple and repeatable call structure, rather than having to perform complex setup every time the same file is opened (I use the sounds in various loops and need to not have to constantly worry about if a channel is full or other factors). It also needs to be relatively small; I don't want to import an entire GUI toolkit just so I can play a small bell and alert sounds. Lastly, it needs to be non-blocking; I need it to play the file once and move on without locking up the entire program.
I had been using PyAudiere in order to play MP3 files from python, as it used to meet all these criteria. Unfortunately, the package has been abandoned, and so I need to find a replacement (PyAudiere was updated to python 2.6 and still works in 2.7 if you hack it by manually changing the version check, but this is not a long term solution, especially since finding a copy of the package is now difficult).
Hi Try the following libraries:
PyMedia
pygame
PyAudio
pyglet

Combine images as frames to make a video with Python

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.

Any Python modules (for Windows and Unix-like) to extract info from video/audio files?

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.

GStreamer: status of Python bindings and encoding video with mixed audio

I am hoping to find a way to write generated video (non-real time) from Python and mix it with external audio file (MP3) simultaneously.
What's the current status of GStreamer Python bindings, are they up-to-date?
Would it be possible to write MPEG-4 output with GStreamer and feed raw image frames from Python
Is it possible to construct pipeline so that GStreamer would also read MP3 audio and mix it into the container, so that I do not need to reprocess the resulting video track with ffmpeg etc. external tools to have the audio track
Are there any up-to-date tutorials for using GStreamer with Python? (I couldn't find anything dated since 2006-2009)
(my old question: did not really give good pointers Writing video with OpenCV + Python + Mac )
Whether or not the binding are "up-to-date" really depends on what version of Python you're using. As for Python 2.7, I am using GStreamer without incident.
I have been fighting a major bug in developing with Python 2.7 and GStreamer on Windows 7 (WinBuilds installers), but I'm able to work with GStreamer just fine on Ubuntu.
GStreamer does have mp3 codecs, but there are some legal matters surrounding their legality in some countries. I'd do a Google search on that before using them.
As for tutorials, no luck. All the same, the existing tutorials do quite well for the modern version, especially this one and this one.
In regards to writing MPEG-4 output and feeding raw images, I do not know. That would be a good stand-alone question, in all honesty.

Categories