Breaking a video into frames with python - python

I am trying to write a program that deletes frames of a video that don't have a particular symbol in them. My general plan:
Split the audio from the video
Split the video into frames
Run the frames through a subroutine that looks for the symbol, by checking the pixels where it should be for being the correct color, and logging the ones that don't.
Delete those frames and corresponding audio seconds
Splices it all back together.
I need some help finding libraries that can do this. I was wondering if wxpython could do the detection of pixel color. I have no idea what library could split audio and video and which could edit audio. I know ffmpeg could split the video into frames but after two days of work I still have not been able to install it for python 2.7, so I either need a way to install it or a different library to do it. Any ideas?

wxPython is mainly for desktop GUI development. I would look at pyAudio for the audio bit or possibly one of the following:
http://xoomer.virgilio.it/sam_psy/psych/sound_proc/sound_proc_python.html
https://github.com/Rudd-O/python-audioprocessing/
http://code.google.com/p/pyo/
You might be able to use one of the Mplayer wrappers for the video:
http://pypi.python.org/pypi/MplayerCtrl/
http://code.google.com/p/python-mplayer/
Or check out that OpenCV project that Sergey mentioned:
http://opencv.willowgarage.com/documentation/python/index.html

Related

How to make screenshots from TS video stream?

I would like to make screenshots each 1 minutes from the video stream. The video stream is provided as m3u8 file:
#EXTM3U
#EXT-X-TARGETDURATION:6
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:112076
#EXT-X-PROGRAM-DATE-TIME:2019-03-19T16:16:53Z
#EXTINF:6.000, 2019/03/19/16/16/53-06000.ts
#EXTINF:6.000, 2019/03/19/16/16/59-06000.ts
#EXTINF:6.000, 2019/03/19/16/17/05-06000.ts
#EXTINF:6.000, 2019/03/19/16/17/11-06000.ts
I found a library to parse it - https://github.com/globocom/m3u8. But I don't understand how I can convert this TS video stream to single jpeg file.
Am I supposed to
download TS file
find needed frame
extract it
delete ts file?
Should I use OpenCV or is there any easier solution?
use OpenV
This is a job for ffmpeg.
To capture a frame from a playlist every minute, you can use:
ffmpeg -i "http://cam.l-invest.ru/nagatinskaya4/tracks-v1/index.m3u8" -vf fps=1/60 invest.ru_%04d.jpg -hide_banner
The above will produce:
invest.ru_0001.jpg
invest.ru_0002.jpg
and so on... once every 60″
Notes:
invest.ru_0002.jpg was taken exactly 60″ after invest.ru_0001.jpg, as you can see in the upper-right timestamp.
-vf indicates ffmpeg to use a video filter fps=1/60, so it will extract one frame every 60″ (src).
The output format and filename structure can be changed if needed (ex: %Y-%m-%d_%H-%M-%S.jpg). Please check the ffmpeg image2 docs for available options.
I think you can use VLC to do that.
EDIT: looks very similar to https://superuser.com/questions/1379361/vlc-and-m3u8-file.
The following answer might not work for your file format (unless higher versions of VLC work correctly ...). May be have a look to this question which might give you some more insight
To my knowledge, VLC works fines with TS files/streams
Once you have a TS file, you should be able to use vlc to perform your screenshots.
According to this link and to this SO question and answers, one can launch VLC and make it perform screen captures.
And according to VLC documentation, it seems possible.
Should work on win/linux/mac.
I do have tested it yet, I need to reach my personal computer to do that.
Quoting:
With new VLC versions (VLC 1.1.0 and above), the thumbnails are generated with scene video filter
vlc C:\video\to\process.mp4 --rate=1 --video-filter=scene --vout=dummy --start-time=10 --stop-time=11 --scene-format=png --scene-ratio=24 --scene-prefix=snap --scene-path=C:\path\for\snapshots\ vlc://quit
If you want to get rid of the sound you can add "--aout=dummy" next to "--vout=dummy".
For older VLC versions (1.0.0 and below) the same can be done with image output module
vlc C:\video\to\process.mp4 -V image --start-time 0 --stop-time 1 --image-out-format jpg --image-out-ratio 24 --image-out-prefix snap vlc://quit
What it does:
When VLC media player runs it 'plays' the video for one second without actually showing the video on screen, and then quits, leaving us with a file named 'snap000000.jpg', containing an image of the first frame of the video.

Display PiCamera video to an embedded video player in a python program

Is there any way of embedding a video player into the GUI of a python program and then displaying video from a piCamera to it in real time?
Currently, when I preview it fills the whole screen and makes any GUI unusable.
I would like to overlay information over the video capture.
I'm not too hot on Python but on Visual Studio for example you could use the VLC plug-in and use it as a display component.
There's lots of tutorials about how to host video and stream it to a server but nothing covers local display.
Cheers.
I'm not sure if this is exactly what you are looking for, but I use RPi-Cam-Web-Interface. If you are not aware of it you may want to take a look: http://elinux.org/RPi-Cam-Web-Interface

Is it possible to save in a file an animation created with Tkinter?

I wanted to use Python to create animations (video) containing text and simple moving geometric objects (lines, rectangles, circles and so on).
In the book titled "Python 2.6 Graphics Cookbook" I found examples using Tkinter library. First, it looked like what I need. I was able to create simple animation but then I realized that in the end I want to have a file containing my animation (in gif or mp4 format). However, what I have, is an application with GUI running on my computer and showing me my animation.
Is there a simple way to save the animation that I see in my GUI in a file?
There is no simple way.
The question Programmatically generate video or animated GIF in Python? has answers related strictly to creating these files with python (ie: it doesn't mention tkinter).
The question How can I convert canvas content to an image? has answers related to saving the canvas as an image
You might be able to take the best answers from those two questions and combine them into a single program.
I've accomplished this before, but not in a particularly pretty way.
Tl;dr save your canvas as an image at each step of the iteration, use external tools to convert from image to gif
This won't require any external dependencies or new packages except having imagemagick already installed on your machine
Save the image
I assume that you're using a Tkinter canvas object. If you're posting actual images to the tk widgets, it will probably be much easier to save them; the tk canvas doesn't have a built-in save function except as postcript. Postscript might actually be fine for making the animation, but otherwise you can
Concurrently draw in PIL and save the PIL image https://www.daniweb.com/software-development/python/code/216929/saving-a-tkinter-canvas-drawing-python
Take a screenshot at every step, maybe using imagegrab http://effbot.org/imagingbook/imagegrab.htm
Converting the images to to an animation
Once the images are saved, I used imagemagick to dump them into either a gif, or into a mpg. You can run the command right from python using How to run imagemagick in the background from python or something similar. It also means that the process is implictely run on a separate thread, so it won't halt your program while it happens. You can query the file to find out when the process is done.
The command
convert ../location/*.ps -quality 100 ../location/animation.gif
should do the trick.
Quirks:
There are some small details, and the process isn't perfect. Imagemagick reads files in order, so you'll need to save the files so that alphabetical and chronological line up. Beware that the name
name9.ps
Is alphabetically greater than
name10.ps
From imagemagick's point of view.
If you don't have imagemagick, you can download it easily (its a super useful command-line tool to have) on linux and mac, and cygwin comes with it on windows. If you're worried about portability... well... PIL isn't standard either
There is a way of doing that, with the "recording screen method", this was explained in other question: "how can you record your screen in a gif?".
Click the link -->LICEcap : https://github.com/lepht/licecap
They say that it's free software for Mac (OS X) and Windows
You could look at Panda3D, but it could be a little over killed for what you need.
I would say you can use Blender3d too but i'm not really sure of how it works. Someone more experimented then me could tell you more about this.

Edit Quicktime file header - audio tracks assignment flags, Timecode track

I use Python quite a bit and am looking for a way to edit a Quicktime file flags via Python / command line. I need to edit the audio tracks assignment flags, Left, Right, Left Surround, etc, also edit Timecode track, Pixel Aspect ratio, etc, quicktime atoms. I have been looking for some time of how this can be done programatically via python but can't find anyway, I don't wan't to re-encode the file, just change the flags. can PyQt do this?
I've never tried pyglet, but it might look like what you need.
pyglet: a cross-platform windowing and multimedia library for Python.
Load images, sound, music and video in almost any format. pyglet can optionally use AVbin to play back audio formats such as MP3, OGG/Vorbis and WMA, and video formats such as DivX, MPEG-2, H.264, WMV and Xvid.
Take a look at the pyglet documentation for more information.

Python/pygame - turning images into video

I am using the Pygame module in python to take pictures with my webcam. The problem is that I would like to export a video file (don't care what type) to use elsewhere. Since pygame cannot export video directly, I guess that there is two ways to do it:
Somehow stitch the photos Pygame creates into a video. (my preferred method)
Use an external library.
I only need 4 frames per second, and I don't care about the picture quality.
How can I make a video with python / pygame?
I have the same problem and am searching the solution.
I found this
This seems work well though I didn't try yet.
Hope this helps.

Categories