lundi 26 août 2019

redCV and FFmpeg: Video reading

redCV and FFmpeg: Video reading

As previoulsy explained, Red and FFmpeg collborate rather well for video and audio recording. In this new post, we'll focus on video reading.

A simple approach: Use ffplay tool

A simple, but efficient way, is to call ffplay tool which is included in FFmpeg framework. Just open a terminal session, and give the name of the movie to read.
ffplay Wildlife.wmv


While running movie, different options can be used form controlling the ouput:
f: for fullscreen
m: toggle audio
p, space: pause/resume the movie
/, *: decrease and increase volume respectively
During the reading of the movie, ffplay returns a lot of information about the video:


Of course, this command can be integrated in Red code as parameter of call function, and GUI program can be developped to avoid command-line use (see /video/ffplay.red code). It is important to use /shell refinement for call, since ffplay uses the terminal to display different information:

 call/shell "ffplay Wildlife.wmv"


This simple approach is very confortable for reading and listening all supported video files and, this is the way I prefer for reading movies without using VLC or Quick Time Player.

A second approach: Extract frames from a movie

In many applications, I don't need tp process audio channel, but I have to focus on images in order to make redCV image processing on time-lapse videos. 
To turn a video to number of images, run the command below. The command generates the files named image0001.png, image0002.png and so on.
ffmpeg -i filename image0d.png
This command is included in video/movies.red program and, associated to Red code for creating an efficient video reader with a clean GUI interface. T

he code is very simple, but contains some interesting functions.
First of all, to create elegant navigation buttons, we profite the fact that Red supports unicode string :)


The second important function concerns the generation of line-command for FFmpeg.


FFmpeg options are very simple:
"/usr/local/bin/ffmpeg" ;location of ffmepg binary
" -y" ;automatically replace image files
"-f image2" ;The image file muxer writes video frames to image files 
" -s 720x480" ;output size
" -q:v 1" ;use fixed quality scale (1 to 31, 1 is highest)
" -r " frames per sec ;fps (mandatory for .vmw files)
" " dataDir ;temp destination directory"img-%05d.jpg" 
;automatic file name numbering
The rest of the code is pure Red and very easy to understand. First of all, you have to select a movie file. When done, FFmpeg is called to create, in a temprary directory, all jpg images corresponding to the number of frames contained in the movie. You can also play with the FPS to create more or less images. Navigation buttons, slider and, text-list faces can be used to directely access any image. When you press the Play/Stop button, for a complete reading, text-list face is disabled. 

Aucun commentaire:

Enregistrer un commentaire