Convert wav to ogg vorbis in Python - python

How can I transcode a wav file to ogg vorbis format using Python?
I can convert to mp3 using PyMedia 1.3.7.3, but when I set the output stream type to 'ogg', I get the error: oggvorbis_encode_init: init_encoder failed and the script dies.

From PyMedia's website:
OGG( optional with vorbis library )
You need to install Vorbis in order for the OGG encoder to work. Since the old version of your question tells me that you're on windows you can grab it here:
http://www.vorbis.com/setup_windows/

Related

PyAv av.open() specify used codec

When PyAv is used to open the alsa audio device. How I can specify used codec and not the ffmpeg default one because that is wrong. By default it will use pcm_s16le and I need to use pcm_s32le. I can record from my device with following ffmpeg command:
ffmpeg -f alsa -acodec pcm_s32le -i dmic_sv alsaout.wav
but not with
ffmpeg -f alsa -i dmic_sv alsaout.wav
Which will give me following error:
[alsa # 0x12061c0] cannot set sample format 0x10000 2 (Invalid argument)
dmic_sv: Input/output error
How to transfer the working command to PyAv av.open() function? There is stream_options but it doesn't seem to work. I tried
stream_options = [{'-acodec': 'pcm_s32le'}]
av.open('dmic_sv', format='alsa', mode='r', stream_options=stream_options)
And I get the same as above.
av.error.OSError: [Errno 5] Input/output error: 'dmic_sv'; last error log: [alsa] cannot set sample format 0x10000 2 (Invalid argument)
How to do this?
I'll answer my own question because I figured it out. I read ffmpeg source code and saw that when using alsa audio device and codec is not specified ffmpeg will default to use signed 16-bit pcm samples. Code here. By further exploring the source code the codec value comes from AVFormatContext::audio_codec_id struct field.
Now figuring out that PyAV using Cython to use FFmpeg and by reading PyAV source code of Container class I noticed it holds AVFormatContext in it's self.ptr variable. Then reading InputContainer source code and especially before calling avformat_open_input function to open the alsa device. Specifying the used audio codec is not supported by PyAV.
I forked the library and ended quickly hacking the solution for me. Now the question is would it be possible to add this feature to PyAV to force the codec used for the audio? In this case when the device is using pcm samples and relying ffmpeg to use choose the default one it will always use 16-bit samples and in my case I needed to use 32-bit samples.
Hopefully this helps someone and save them the trouble I went through :) I also posted this same answer for PyAV issue here.

Could not open codec 'libopenh264': Unspecified error

I am using OpenCV to process videos for my research. I have Python 2.7 and OpenCV 3.2 versions installed on Windows 10. When I do background subtraction on a video in Python using OpenCV, it works fine and produces the output. However, when I try to save the background subtracted video, it throws this error:
warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:779)
warning: MAH00119.avi (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:780)
OpenCV: FFMPEG: tag 0x34363258/'X264' is not supported with codec id 28 and format 'h264 / raw H.264 video'
Failed to load OpenH264 library: openh264-1.6.0-win64msvc.dll
Please check environment and/or download library: https://github.com/cisco/openh264/releases
[libopenh264 # 0000000001f5bf60] Incorrect library version loaded
Could not open codec 'libopenh264': Unspecified error
I am processing MP4 videos. And I followed the instructions carefully while installing ffmpeg, like adding the bin's path to environment variables. I don't know what else to do. Stuck on this for three days now.
Any help would be much appreciated!
Thanks in advance!!
The error message you are getting says that openCV can't load the H264 codec. H264 doesn't come by default with the default installation of openCV.
To add the H264 codec download 'openh264-1.6.0-win64msvc.dll.bz2' from https://github.com/cisco/openh264/releases/tag/v1.6.0.
Extract the file and move the extracted DLL to the same directory as your python file. OpenCV should now be able to find the DLL and load the H264 codec.
Note that in your error message, openCV is looking for the openh264-1.6.0-win64msvc.dll (failed to load: openh264-1.6.0-win64msvc.dll.bz2) which is what we have now provided.

Opencv failed to parse AVI

I've installed opencv3.1.0 through anaconda along with the ffmpeg package. opencv still gives
Failed to parse avi: index was not found
when I try to read an .avi file.
Is it possible to have .avi files work straight out of anaconda or do I have to compile everything manually (a process I have not have had much luck with so far).

python check audio file type, MP3 or FLAC

I want to check if a audio file to see if its MP3 or FLAC the checks only have to be basic but I want to go beyond simply checking the file extensions
os.path.splitext
Works okay but no good if the file doesn't have an extensions written in or someone passes a file with a fake extension
I've tried but it just returns None
sndhdr.what(file)
I've also tried using magic but it returns 'application/octet-stream' which isn't much use.
magic.from_file(file, mime=True)
I've read Mutagen could be good for this but so far failed to find any function that outputs the audio encoding as MP3 or FLAC
To find the File format including audio, video, txt and more, you can use
fleep python library to check the audio file format:
First you need to install the library:
pip install fleep
Here is a test code:
import fleep
with open("vida.flac", "rb") as file:
info = fleep.get(file.read(128))
print(info.type)
print(info.extension)
print(info.mime)
Here is the results:
['audio']
['flac']
['audio/flac']
Even if the file doesn't have an extension or someone passes a file with a fake extension, it will detect and return.
Here I have copied the vida.wav file to dar.txt and also removed the extension
import fleep
with open("dar", "rb") as file:
info = fleep.get(file.read(128))
print(info.type)
print(info.extension)
print(info.mime)
The result is still the same.
['audio']
['flac']
['audio/flac']
Credit to the author of the library
https://github.com/floyernick/fleep-py
This might help you getting started
21.9. sndhdr — Determine type of sound file
https://docs.python.org/2/library/sndhdr.html
Try the library filetype. filetype on pypi. filetype on github.
(Another answer mentioned fleep. I tried that. But unfortunately, it looks like it isn't maintained anymore. It doesn't recognise all the types of mp3, for example. filetype is actively maintained and recognises all the mp3 formats)
Install filetype
pip install filetype
Use filetype to recognise your file. It uses file signatures, a.k.a magic bytes at the start of the file.
import filetype
kind = filetype.guess('path/to/sample.mp3')
if kind is None:
print('Cannot guess file type!')
else:
print('File extension: %s' % kind.extension)
print('File MIME type: %s' % kind.mime)
NOTE, there is an issue
It seems like the filetype repo while actively maintained is slow to make releases. I raised an issue on GitHub about the slow releases here.
This means to get the new bits of the library (like more file signatures to match with), you might want to install directly from the git repo.
To do this with pip:
pip install -e git+https://github.com/h2non/filetype.py
To do this with pipenv:
pipenv install -e git+https://github.com/h2non/filetype.py#egg=filetype
(-e means install in editable mode. That comes as a recommendation from here.)
You can read file specifications of mp3 and flac and then can implement a checker that reads and parses mp3 as a binary file.A modifiable example with a similar goal is here

Pillow cannot read different TIFF files

I'm using Pillow 2.2.1 (installed it with pip) on Ubuntu Server 12.04 64-bit and trying to batch resize images into jpeg format. I've also installed both zlib1g-dev and libtiff-dev with apt-get.
I use the following line
Image.open(path/to/image)
to open the image files (jpeg and tiff's). I can open some tiff images, but for others I just get the following error:
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2006, in open
raise IOError("cannot identify image file")
Since this doesn't occur for every tiff image I try to open does this mean that these files are corrupted? I have no problem opening the images in question with windows photo viewer or even with GIMP though.
Note: All the images were just given to me by my client so I have no idea how he digitized the images (scanned or took a picture of them is my best guess), or if that even matters.
Am I missing a package or dependency, or is there really just something about the image files that I'm not seeing?
TIFF is only a container format (like AVI on the video). The file extension does not actually signify how the image data inside is encoded. Most professional photo editing applications create their own flavour of TIFF which is unreadable in other software. PIL might support only certain subset of TIFF flavours (e.g. uncompressed).
If your system is a web upload style system I suggest you stop accepting TIFF format to avoid problems altogether.
More information: http://en.wikipedia.org/wiki/Tagged_Image_File_Format

Categories

Resources