What is a good Python library for managing video frame frames?

I am looking for a Python video processing library similar to PIL , where I can iterate over all frames of the original video, access pixel data for each frame, draw on each frame, and save the result as a new video file.

I found a couple of similar questions, but they are pretty old:

They recommend PyMedia and PyFFMPEG . PyMedia seems pretty dated (but can it still work?), And PyFFMPEG, although later, has almost no documentation.

I was not lucky to install them on Ubuntu 10.10, before I click, there are:

a) The best library I should look at?

b) Good instructions on how to start any of them?

+7
source share
4 answers

I often need the same thing, and as far as I know, there is no good solution with bindings in Python.

And it’s not as easy as it might seem to manipulate the frames of a video file. The modern file format for video does not save frames one frame after another, but instead uses "delta frames" in which only changes from one frame to another are stored. Other considerations, such as video with a variable frame rate, make the task difficult.

In the past, I used the following command to create images from a video.

ffmpeg -i /path/to/file.mpg -an -r 30 -s 320x240 tmp%06d.jpg 

Where 30 is the target frame rate, 320x240 image size and the tmp% 06d.jpg template used to store the generated jpeg. Then you can use PIL to control each frame and mencoder or ffmpeg to re-run the images in the movie:

 ffmpeg -r 30 -i tmp%06d.jpg output.mpg 

Obviously, you will lose the soundtrack.

+7
source

I looked at using py.processing for similar work. It does everything you ask, but is a hybrid with processing. You are not writing python code as such. In any case, it is very easy to work with it, but the overhead of the program / interpretation is very much, so it can be slow to make changes to films in real time. You said you want to modify the file so that it works.

+2
source

I recommend scikit-video for you, which is the simplest python video processing library I've ever met.

This is the official introduction to scikit video from the project website:

Scikit-video is designed to easily process video using Python. It is modeled in the spirit of other successful scikits such as scikit-learn and scikit-image. Developers of renowned scikit-video libraries exist for video management such as PyFFmpeg, MoviePy, PyAV, imageIO and opencv. However, no libraries have been found to provide an all-in-one solution for research-level video processing tools.

+1
source

You can use my Python VidGear library to process the WriteGear API video , which allows us to use almost all available parameters supported by FFmpeg (frame rate, bit rate, codecs, format and size, multiplexing, DEMUX, etc.) in compression mode , easily and flexibly, and at the same time reliably processes errors and warnings all the time very quietly.

Parameters:

For example, to use H.264 to produce high-quality video using the x264 encoder, we can configure its parameters as follows to obtain lossless video output:

  output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast", "tune": "zerolatency"} 

and then pass this dictionary to WriteGear as an example below

Basic usage example

 # import libraries from vidgear.gears import WriteGear import cv2 output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast"} #define (Codec,CRF,preset) FFmpeg tweak parameters for writer stream = cv2.VideoCapture(0) #Open live webcam video stream on first index(ie 0) device writer = WriteGear(output_filename = 'Output.mp4', compression_mode = True, logging = True, **output_params) #Define writer with output filename 'Output.mp4' # infinite loop while True: (grabbed, frame) = stream.read() # read frames # check if frame empty if not grabbed: #if True break the infinite loop break # {do something with frame here} gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # write a modified frame to writer writer.write(gray) # Show output window cv2.imshow("Output Frame", frame) key = cv2.waitKey(1) & 0xFF # check for 'q' key-press if key == ord("q"): #if 'q' key-pressed break out break cv2.destroyAllWindows() # close output window stream.release() # safely close video stream writer.close() # safely close writer 

Read more about usage here and full documents here

0
source

Source: https://habr.com/ru/post/885636/


All Articles