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.
Related
I made this thread in Raspberry PI Stack Exchange and my problem was solved. Until last week I tried to run my Python application again (both in Raspbian and Ubuntu 16.04) this error appeared again.
OSError: [Errrno -9996] Invalid input device (no default output device)
What I have done so far.
pyaudio.PyAudio().get_device_count() returns 0.
arecord --device=hw:1,0 --format S16_LE --rate 44100 -c1 test.wav returns this error.
ALSA lib pcm_hw.c:1700:(_snd_pcm_hw_open) Invalid value for card
arecord: main:722: audio open error: No such file or directory
I have properly installed PortAudio version 19 stable.
I can still use my microphone for other activities like video call, ....
I am using Python 3.
Well the problem is definitely in the PyAudio part. However, some threads mention that the main culprit is miss-connection between PyAudio and PortAudio (although, I have already compiled proper PortAudio version 19 stable).
At this point my solution is to use pyalsaaudio from https://github.com/larsimmisch/pyalsaaudio. For Python 2.x you can just install it with pip install pyalsaaudio, however for Python 3.x you need to compile it from the source codes (see instruction in its GitHub page). Note to mention is that pyalsaaudio only works for Linux. With pyalsaaudio my Python application is working like usual.
This is not really intended to be an answer but it might be helpful to print a list of all the devices available to pyaudio and see if your USB soundcard is even being recognized.
(Some code from a previous project):
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):#list all available audio devices
dev = p.get_device_info_by_index(i)
print((i,dev['name'],dev['maxInputChannels']))
You may also want to look into alsa and some techniques to list available devices and possibly alsamixer as well.
I have some troubles in interpreting the documentation (http://iepy.readthedocs.io/en/latest/tutorial.html) commands of the Python IEPY package for Natural Language Processing. The first step to work with IEPY is to create an "instance" (as a non-programmer I have little idea what is it all about). They also provide the command to do it:
iepy --create <project_name>
My problem is that when I type the exact sentence into my command line, I get a "syntax error". I suppose that I do not follow some coding conventions and misinterpret what I should really type. Would be glad to hear what I do wrong.
Another source of the problem could be an improper installation of some of the additional libraries required for IEPY. After trying to install IEPY using
pip install iepy, I got a big fat error message after "collecting django-angular-0.7.8". The command line reports an "Exception", gives a reference to the line, where it happens and describes the source of an error (this is how I interpret the output at least). Which is:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position
4: invalid start byte
Here is the screenshot (cannot use copy-paste in my command line)
Installation of IEPY: The Error Message While Installing IEPY
P.S. It looks that the documentation in general is Linux-users oriented while I am using Windows 8.1. Is it the source of my troubles?
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.
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
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/