Could not open codec 'libopenh264': Unspecified error - python

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.

Related

Why am I getting "`OSError: unknown file format`" when opening a TTF with `PIL.ImageFont`?

In Google Colab I am using PIL (Python Imaging Library). I have uploaded a TrueType Font MSMINCHO.TTF.
I am attempting to read it with fnt_kanji = ImageFont.truetype('./MSMINCHO.TTF') but I get the error "OSError: unknown file format".
I know the font is valid because it displays correctly in my local system. And the encoding is Unicode as it should be, since this font contains kanji (Chinese characters). And the filepath is also valid and correctly cased (otherwise, "OSError: cannot open resource" is thrown instead).
I've been searching for the solution in the docs and the web for more than half an hour and haven't found it.
Try changing your line to:
fnt = ImageFont.truetype('./MSMINCHO.TTF', layout_engine=ImageFont.LAYOUT_BASIC)
if that doesn't work you can try refreshing your pillow module like this:
pip uninstall pillow
python3 -m pip install -U https://github.com/python-pillow/Pillow/archive/master.zip

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.

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).

how to get a video file's orientation in Python

I would like to load a video file's frames into a numpy array. I want the frames to be properly upright, which means I need to read the orientation metadata in the video file, and rotate the loaded frames accordingly.
I have a means of loading the frames (opencv's python bindings), so all I need is a way to read the video file's orientation metadata.
I'm trying to read a .MOV file recorded off my iPhone, in python running on an Ubuntu 14.04 machine.
Stuff I've looked at:
opencv: no way of inspecting video orientation
hachoir-metadata: doesn't display orientation metadata
enzyme: only works on .mkv files, AFAICT.
ffmpeg: no longer available as a stand-alone executable on Ubuntu's repositories. This is a dealbreaker since I'd rather just hand-specify the rotation rather than go through the trouble of compiling ffmpeg from source just to get video orientation.
PIL, pyexiv: AFAICT, only loads image files.
I'm running the code on an Ubuntu 14.04 machine, so I'm looking for libraries or command-line programs available on that platform.
Any pointers would be greatly appreciated.
We use MediaInfo (libmediainfo)
it is running both on Windows and Ubuntu, and you can get all you need (codec, aspect, fps, bitrate, orientation...).
use qtrotate. It's just one file, and it works on .Mov files
https://github.com/danielgtaylor/qtrotate

full built file support PYGAME

I cant load images thats not BMP files in pygame.
Ive searched everywhere for solution but I couldn't find one.
In pygame site they say this:
The image module is a required dependency of Pygame, but it only
optionally supports any extended file formats. By default it can only
load uncompressed BMP images. When built with full image support, the
pygame.image.load - load new image from a file function can support
the following formats.
I couldn't understand what to do, and how can I get the full build image support.
I am running python 3.3 on ubuntu 13.04.
I'm new to python.
help?
the error:
File "/home2/tor/workspace/PYGAME/src/Main.py", line 13, in <module>
ball = pygame.image.load("/home2/tor/Downloads/ball.gif")
pygame.error: File is not a Windows BMP file
BTW I also tried this on jpg file.
Your version of Pygame may not be sufficient for Python 3 on your system, I believe ubuntu 13.04 comes with Python 3 installed by default, I am not sure if it has Python 2, but if it did happen to have it, I would use that instead.
Information from this question: PyGame.error in ubuntu
Right below where it says "can support the following formats" the list says:
JPG
PNG
GIF (non animated)
BMP
PCX
TGA (uncompressed)
TIF
LBM (and PBM)
PBM (and PGM, PPM)
XPM
Although it does say it is not a Windows BMP file, that does not mean it has to be bmp. I do not believe gif is supported when animated as the list says.
Here is an example from the pygame comments for a png image.
char_surf=pygame.image.load(os.path.join('data', 'char.png'))

Categories

Resources