I have a couple of video container files which contain audio and video in various codecs. Now I'd like to inspect the container from a Python script to know which codec is used for audio+video. This is on a linux box so I have all the tools available if necessary.
I thought that maybe gstreamer could help me here but I was unable to find an API which could help me here.
Any ideas? I'm also open for any suggestion, doesn't need to be gstreamer as long as it is free software :-)
fs
ffprobe -show_format -show_streams -loglevel quiet -print_format json YOUR_FILE
Just invoke this with subprocess.check_output and you'll get a beautiful JSON description of your media file. If you need it to grab the data from stdin, replace YOUR_FILE with pipe:0.
ffprobe comes with ffmpeg.
Try downloading the ffmpeg source and look at the source for their command line programs. I've hacked up similar utilities in the past. I'm not posting my solution because ffmpeg likes to change their API, so my old code is unlikely to compile with the current version. You'll want to do enough work to create codec context, which you can inspect to get what you need.
Some other alternatives:
MediaInfo: http://mediainfo.sourceforge.net/en
GSpot (Windows only): http://www.headbands.com/gspot/
EDIT:
http://code.google.com/p/pyffmpeg/ might have what you want (I haven't used it myself).
You can use decodebin2 in Gstreamer. Take a look at TAE for code examples.
Related
So, I am a beginner in Pyhton and recently I have covered what would be the basics of the language.
And now I have this little project in mind which is basically to create a script that can convert files in a massive way. Specifically convert .ogg files into .mp4 or .mkv files. The intention of this is to convert whatsapp audio files that come in .ogg format to make them more manipulable.
I would like hints suggestions and guidance on how I could do this. Which lib could I use to help me and where can I learn more about this and file conversion using python
Not a python user, but will try to give you some direction.
There is a software called ffmpeg. It can be used as a command line tool to convert any audio/video files to almost any format. If you decide to use it manually then you need to download binaries here, put your .ogg file in the /bin directory next to ffmpeg.exe file and execute (here is the screenshot for better understanding):
./ffmpeg.exe -i input.ogg output.mp4
or:
./ffmpeg.exe -i input.ogg -c copy output.mp4
These are only basic commands, for more examples check this answer.
But I would suggest to simply use a python wrapper of this tool that is already implemented, check this quick start guide with lots of examples in python. For more details this answer can be also helpful.
If I download an audio file from the web and something bad happens to the download process, how does one efficiently detect that the audio file is incomplete with python?
There are some ideas, such as using the file command in linux:
file audio.mp4
But it recognizes that it's mp4:
audio.mp4: ISO Media, MPEG v4 system, version 2
Even mplayer detects the mp4 audio type, but fails when trying to play. I don't think launching mplayerfrom python and checking if it failed is a scalable solution though.
Here is a sample of broken file:
https://www.dropbox.com/s/5rpscb9r1xrrx4t/They
The sample above fails with mutagen and mp4file, causing them to hang indefinitely. It has to do with fileObject.tell().
There are many different audio file formats, and container formats for things that that may or may not be audio files.
Fortunately, there are libraries that can a wide variety of different kinds of files. And there are Python wrappers for:
Portable command-line tools like ffmpeg and mplayer.
Portable libraries like libavcodec (what ffmpeg uses).
Platform-specific libraries like Core Audio or QuickTime or Windows Media.
If you're willing to use separate wrappers for separate file types, there are even more choices (e.g., libmp4v2 is great for MP4 files, but useless for anything else).
Of course there are huge tradeoffs—the more powerful libraries are often going to be more complex, or have more prerequisites. Do some searching at http://pypi.python.org/ to see what turns up; you should be able to find something that does everything you want.
For one really simple example, mp4file will attempt to parse any MPEG4 container. If it's incomplete, or has any invalid atoms, you'll get an exception. So, the check is just one line, mp4file.Mp4File(path). If it succeeds, it's complete; if it throws an exception, it's incomplete or invalid. But of course this will accept a complete MPEG4 video file, or MPEG4 with no audio or video in it, and it will reject a complete MP3, or even a complete M4A with one broken metadata tag.
I'm trying to capture a video using a webcam then encoding it as an mp42 asf file on a windows machine.
I managed to encode an mpeg2 file using pymedia but pymedia doesn't seem to support mp42.
I installed opencv and tried to use the python wrapper but python keeps crashing everytime I create a writer. Even with just captureing images, it seems too slow and unreliable.
Does anyone know of a python module that allow me to create mp42 files?
Thanks,
-Ray
According to this : http://forums.creativecow.net/readpost/291/493 mp42 is a version 2 of MP4 ISO file format. (Explained here)
Where as according to this wiki link, it is an old Microsoft's (proprietary) MPEG4 codec version 2. This was built-in under VFW (video for windows). I think FFMPEG allows encoding using -vcodec MSMPEGv2 (or something like that) as per this link: http://ffmpeg.org/general.html#Video-Codecs. Though i am not sure if this is same.
If FFMPEG works, it has python binding pyffmpeg that might work for you.
I'm looking for python code that can convert .wav or other format to FLAC or mpeg. I hope there is one that doesn't depend on other binaries or libraries and just pure python so that it can run independently anywhere where python is installed ie also serverside. Do you know any examples?
Thanks
Python Audio Tools seems to fit your description.
Worst case, you'd have to rip the source code out and go from there.
As others have said, there are other alternatives.
I need to decode an MP3 file with Python. What is a good library which can do that?
I looked at PyMedia but the project seems dead and it didn't worked on MacOSX. Then I found out about pyffmpeg but I didn't got it working on MacOSX so far.
Any suggestion?
I did try an easy_install of PyMedia on OS X / Fink, and it did not work because it could not find the source. This module does look quite dead…
One way of decoding MP3 is to call ffmpeg without going through pyffmpeg, but by calling ffmpeg using the standard subprocess module instead.
You really need an external library. It'd be very difficult to do in Python with any sort of speed - see How to convert MP3 to WAV in Python for some discussion.
How about python-mad? MAD being the 'mpeg audio decoder'; there's a python library. It'd give you the audio data. Never used it myself...
I decided to code this myself based on subprocess and ffmpeg.
Some code can be found here:
https://github.com/albertz/learn-midi/blob/master/decode.py
Please try https://github.com/sampsyo/audioread
It's fast, installs from pypi and works well