How to take screenshots from a stream without displaying the GUI?
Posted: 11 May 2016 21:41
I am writing a program for the Raspberry Pi which utilizes libVLC to read rawvideo data from standard input and periodically (roughly twice a second) takes a screenshot from the rawvideo data and saves the image to a particular location.
The program words perfectly on Ubuntu, but when I move it over to the Raspberry Pi it is insanely sluggish and throws errors everywhere. After researching these error messages, it appears to be an issue with VLC being too heavy weight of a program for the Raspberry Pi. The issue though is in the rendering of the image in the VLC display.
The simplest solution I can think of is seeing if libVLC can let you take screenshots of the stream WITHOUT needing to render it in the UI? Is there a way to just have it running in the background/terminal and just take screenshots from behind the scenes?
Thank you for your time, my program is listed below. The line that causes all the rendering has the comment on it.
-----
The program words perfectly on Ubuntu, but when I move it over to the Raspberry Pi it is insanely sluggish and throws errors everywhere. After researching these error messages, it appears to be an issue with VLC being too heavy weight of a program for the Raspberry Pi. The issue though is in the rendering of the image in the VLC display.
The simplest solution I can think of is seeing if libVLC can let you take screenshots of the stream WITHOUT needing to render it in the UI? Is there a way to just have it running in the background/terminal and just take screenshots from behind the scenes?
Thank you for your time, my program is listed below. The line that causes all the rendering has the comment on it.
-----
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <vlc/vlc.h>
#define SCREENSHOTRATE 2
void handleTerminate(int code);
libvlc_instance_t *inst;
libvlc_media_player_t *mp;
libvlc_media_t *m;
int terminate;
int main(int argc, char **argv) {
const char * const vlc_argv[] = {
"--demux", "rawvideo", "--rawvid-height=480", "--rawvid-width=720", "--rawvid-chroma=UYVY",
"--rawvid-aspect-ratio=3:2", "--rawvid-fps=29.97", "--clock-jitter=0", "-Incurse"
};
int vlc_argc = sizeof(vlc_argv) / sizeof(vlc_argv[0]);
signal(SIGINT, handleTerminate);
terminate = 0;
int screenshotResult = 0;
inst = libvlc_new(vlc_argc, vlc_argv);
mp = libvlc_media_player_new(inst);
m = libvlc_media_new_fd(inst, 0);
libvlc_media_player_set_media(mp, m);
libvlc_media_player_play(mp); //This line pops up GUI. Can we remove it and still take screenshots somehow?
//Sleep 2 seconds to let VLC open
usleep(2000000);
while(!terminate) {
usleep(1000000 / SCREENSHOTRATE);
screenshotResult = libvlc_video_take_snapshot(mp, 0, "pic.png", 0, 0);
if (screenshotResult == -1) {
printf("[ERROR] Screenshot failed. Exiting program.\n");
terminate = 1;
}
}
libvlc_media_release(m);
libvlc_media_player_stop(mp);
libvlc_media_player_release(mp);
libvlc_release(inst);
printf("\nResources released successfully\n");
return 0;
}
void handleTerminate(int code) {
terminate = 1;
}