I am trying to make video looper in linux. For that i am using mplayer for videos. My program gets list of videos and images from directory and plays them. My problem is when i call mplayer or image viewer from c with char array parameters it is not working. For example,
system("eog -f /home/user/Desktop/Video/screenshot_0000.png");
or
system("mplayer -fs /home/user/Desktop/Video/video1.mov");
if i call this way it works. But when i get video list from directy and send them with char array it is not working. When i tried system() call it gives 'sh: 1: video1.mov not found' error and when i use exec() fuctions nothing happens.
Here is my code;
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <dirent.h>
#include <string.h>
#define FILE_PATH "/home/user/Desktop/Video/"
char *getExt (const char *fspec) {
char *e = strrchr (fspec, '.');
if (e == NULL)
e = ""; // fast method, could also use &(fspec[strlen(fspec)]).
return e;
}
void get_list(char liste[][150])
{
DIR *d;
struct dirent *dir;
char *path = ("%s.",FILE_PATH);
d = opendir(path);
int counter=0;
/*if(d != NULL)
{
while((dir=readdir(d))!= NULL)
{
// printf("%s\n", dir->d_name);
if(dir->d_type==DT_REG)
file_numbers++;
}
closedir(d);
}
char liste[file_numbers][150];*/
d = opendir(path);
if(d != NULL)
{
while((dir=readdir(d))!= NULL)
{
// printf("%s\n", dir->d_name);
if(dir->d_type==DT_REG)
{
strncpy(liste[counter],dir->d_name,150);
// printf("%s\n", liste[counter]);
counter++;
}
}
closedir(d);
}
}
int main ()
{
int status;
pid_t child_pid, pid_w;
DIR *d;
struct dirent *dir;
char *path = ("%s.",FILE_PATH);
d = opendir(path);
int file_numbers=0;
int counter = 0;
if(d != NULL)
{
while((dir=readdir(d))!= NULL)
{
// printf("%s\n", dir->d_name);
if(dir->d_type==DT_REG)
file_numbers++;
}
closedir(d);
}
char liste[file_numbers][150];
get_list(liste);
/*for(int i = 0; i< file_numbers;i++)
printf("%s\n", liste[i]);*/
while(1)
{
char *ext = getExt(liste[counter]);
//printf("eog -f %s/%s\n",PATH,liste[counter]);
child_pid = fork();
if(child_pid==0)
{
//printf("-eog -f %s/%s\n",PATH,liste[counter]);
if(!strcmp(ext,".JPG") || !strcmp(ext,".jpg") || !strcmp(ext, ".gif") || !strcmp(ext,".png") || !strcmp(ext, ".PNG") || !strcmp(ext, ".GIF"))
{
//printf("eog -f %s%s\n",FILE_PATH,liste[counter]);
char command[1000];
strcpy(command,("eog -f %s%s",FILE_PATH,liste[counter]));
//printf("%s", command);
execl(command,command,NULL,NULL);
sleep(5);
}
else if (!strcmp(ext,".mov") || !strcmp(ext, ".mp4") || !strcmp(ext, ".avi") || !strcmp(ext,".wmv"))
{
//printf("mplayer -fs %s%s\n",FILE_PATH,liste[counter]);
char command[1000];
strcpy(command,("mplayer -fs %s%s",FILE_PATH,liste[counter]));
//printf("%s",command);
execl(command,command,NULL,NULL);
// system(command);
}
else
printf("--%s%s",FILE_PATH,liste[counter]);
}
else
{
pid_w = waitpid(child_pid,&status,0);
// system("eog -f /home/user/Desktop/Video/screenshot_0000.png");
// sleep(5);
// exit(EXIT_SUCCESS);
}
counter++;
if(counter >=file_numbers)
counter =0;
}
}