LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2011, 03:27 AM   #1
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908
Blog Entries: 26

Rep: Reputation: 49
c program does not run batch file properly


Hi ,
I am using windows xp.
I got an idea to create an entry in context menu of all files which invokes a program that plays video using mplayer & autoload subtitle file in corresponding folder (if found) with some -vf options .
Here is the code so far

Code:
//printf("\nflag line 1");
#include<string.h> // for strcat,strcpy,strlen,memicmp,strrev
#include<stdio.h> // for fgets,fopen,fprintf,fclose
#include<stdlib.h> // for atoi,system

char name[128],sf[128],dir[256],fp[128];//sf=subtitle file
int bbh;//bottom band height

void analyzebyffprobe()
{	FILE *fr,*fi;//fr to read g:\\fvh which we will create soon , fi to create batch file
	char cmd[512],vh[4];//vh=video height
	cmd[0]=0;
	strcat(cmd,"ffprobe  \"");
	strcat(cmd,fp);
	strcat(cmd,"\" 1> g:\\fa 2>&1 ");
	fi=fopen("g:\\a.bat","w");
	if(fi==NULL)
		printf("\ncould not open g:\\a.bat to run ffprobe & sed in batch file");
	fputs(cmd,fi);
	cmd[0]=0;
	strcat(cmd,"\nsed  -n \"s/.*\\( [0-9]\\+x\\([0-9]\\+\\)\\).*/\\2/pw g:\\fvh\" g:\\fa ");//fvh=for video height
	fputs(cmd,fi);
	fclose(fi);
	system("g:\\a.bat");
	fr=fopen("g:\\fvh","r");
	if(fr==NULL)
		printf("\ncould not open g:\\fvh to decode height of video");
	fgets(vh,4,fr);
	bbh=600-(atoi(vh));
	printf("\nvideo height is : %d",bbh);
	fclose(fr);
}

int check(char d[128])
{	int len;
	char bkup[128];
	strcpy(bkup,d);
	len=strlen(d);
	if(memicmp((strrev(d)),"trs",3))
	{	printf("\n%s!=trs",d);
		return 0;
	}
	else
	{	printf("\n%d:%s",len,bkup);
		strcpy(sf,bkup);
		return 1;
	}
}

int findsubtitlefile()
{	FILE *fr,*log;//fr=file read,fw=filw
	char fn[128];//fn=file name
	log=fopen("g:\\log","a");
	fr=fopen("g:\\list","r");
	if(fr==NULL)
		printf("\ncould not read g:\\list");
	fprintf(log,"\nwe have to search srt in following \"name-length:file-name\" pairs \n");
	while ( fgets ( fn, 125, fr ) != NULL ) 
	{	fprintf(log,"\n%d:%s",strlen(fn),fn);
		fn[strlen(fn)-1]=0;
		if(check(fn))
		{	printf("\nfound subtitle file");
			return 1;
		}
	}
	fclose(fr);
	fclose(log);
	return 0;
}

void main(int argc,char *argv[])
{	FILE *log,*list;
	int i,j,len;
	char dir[120],command[512],vh[4];
	log=fopen("g:\\log","w");
	if(log==NULL)
		printf ("\nCould not open g:\\log for writing ");
	// fp=full path : copying the full path to log followed by number of words in it
	for(i=1;i<argc;i++)
	{
		strcat(fp,argv[i]);
		strcat(fp," ");
	}
	fp[strlen(fp)-1]=0;
	fprintf(log,"\nfull path=%s\nnumber of words=%d",fp,argc-1);
//copying directory name to 'dir' variable
	len=strlen(fp);
	i=strlen(fp)-1;
	while(fp[i]!='\\')
		i=i-1;
	for(j=0;j<i;j++)
		dir[j]=fp[j];
		dir[j]=0;
	fprintf(log,"\ndirectory is %s",dir);
//copying file name to 'name' variable
	for(j=0,i++;i<len;i++,j++)
		name[j]=fp[i];
		//name[j]=0;
	printf("\nfile name:%s",name);
	fprintf(log,"\nfile name  : %s",name);
//building & executing 'dir' command to output all items in current folder to g:\list
	strcat(command,"ls -1 \"");
	strcat(command,dir);
	strcat(command,"\" > g:\\list");
	system(command);
	fprintf(log,"\nwe have executed : %s",command);
	fclose(log);
	command[0]=0;
//let us find the subtitle file
	if (findsubtitlefile())
	{	//run the file with subtitle
		analyzebyffprobe();
		strcat(command,"k -fs -vf screenshot,expand=0:-");
		vh[0]=(bbh/100)+48;
		vh[1]=(bbh/10)+48;
		vh[2]=(bbh%10)+48;
		vh[3]=0;
		printf("\nwe converted bbh to string : %s actually %c %c %c ",vh,vh[0],vh[1],vh[2]);
		strcat(command,vh);
		strcat(command,":0:0 \"");
		strcat(command,fp);
		strcat(command,"\" -sub ");
		strcat(command,"\"");
		strcat(command,dir);
		strcat(command,"\\");
		strcat(command,sf);
		strcat(command,"\"");
		printf("\nwe will run %s",command);
	}
	else
	{	//run file without subtitle
		strcat(command,"k -fs \"");
		strcat(command,fp);
		strcat(command,"\"");
	}
	system(command);
	printf("\nprogram ends");
	getchar();
}
I also added an entry in registry
key : HKEY_CLASSES_ROOT\*\shell\autoload video with subtitles\command
default string : c:\turboc3\source\pass.exe %1

This uses first srt file found as subtitle.
k (kovensky on feb 12 2010 mplayer.exe) , ls (from unxutils) , sed , ffprobe is binary in system path folder.

Strangely all goes well if subtitle not found.
In other case
(1)the a.bat file created by function analyzebyffprobe runs well if executed by me explicitly in command prompt.
(2)if my program binary runs a.bat using system call then I get

Code:
file name:Absolute Zero.flv
43:Absolute Zero ep01-The Conquest of Cold.srt
found subtitle file
C:\SUMEET\XYPLORER>ffprobe  "I:\video\PRESER~1\serial\absolute zero\Absolute Zer
o.flv" 1> g:\fa 2>&1
FFprobe version SVN-r23607, Copyright (c) 2007-2010 the FFmpeg developers
  built on Jun 15 2010 04:09:35 with gcc 4.4.2
  configuration: --target-os=mingw32 --enable-runtime-cpudetect --enable-avisynt
h --enable-gpl --enable-version3 --enable-bzlib --enable-libgsm --enable-libfaad
 --enable-pthreads --enable-libvorbis --enable-libtheora --enable-libspeex --ena
ble-libmp3lame --enable-libopenjpeg --enable-libxvid --enable-libschroedinger --
enable-libx264 --extra-libs='-lx264 -lpthread' --enable-libopencore_amrwb --enab
le-libopencore_amrnb --enable-librtmp --extra-libs='-lrtmp -lpolarssl -lws2_32 -
lwinmm' --arch=x86 --cross-prefix=i686-mingw32- --cc='ccache i686-mingw32-gcc' -
-enable-memalign-hack
  libavutil     50.19. 0 / 50.19. 0
  libavcodec    52.76. 0 / 52.76. 0
  libavformat   52.68. 0 / 52.68. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libavfilter    1.20. 0 /  1.20. 0
  libswscale     0.11. 0 /  0.11. 0
Argument '1' provided as input filename, but 'I:\video\PRESER~1\serial\absolute
zero\Absolute Zero.flv' was already specified.

C:\SUMEET\XYPLORER>sed  -n "s/.*\( [0-9]\+x\([0-9]\+\)\).*/\2/pw g:\fvh" g:\fa
SED.EXE: can't read g:\fa: No such file or directory


video height is : 592
we converted bbh to string : 5k2 actually 5 k 2
we will run k -fs -vf screenshot,expand=0:-5k2:0:0 "I:\video\PRESER~1\serial\abs
olute zero\Absolute Zero.flv" -sub "I:\video\PRESER~1\serial\absolute zero\Absol
ute Zero ep01-The Conquest of Cold.srt"
program ends
Also the files created by pass.exe (my compiled source code) have following contents
Code:
---a.bat---
ffprobe  "I:\video\PRESER~1\serial\absolute zero\Absolute Zero.flv" 1> g:\fa 2>&1 
sed  -n "s/.*\( [0-9]\+x\([0-9]\+\)\).*/\2/pw g:\fvh" g:\fa 
--fvh-- 0 bytes
---list---
Absolute Zero ep01-The Conquest of Cold.srt
Absolute Zero ep02-The Race for Absolute Zero.srt
Absolute Zero.flv

---log---

full path=I:\video\PRESER~1\serial\absolute zero\Absolute Zero.flv
number of words=3
directory is I:\video\PRESER~1\serial\absolute zero
file name  : Absolute Zero.flv
we have executed : ls -1 "I:\video\PRESER~1\serial\absolute zero" > g:\list
I think , ffprobe (windows binary) is not redirecting error & output to g:\fa (that is why , file was not created . Hence sed could not extract video height from g:\fa & variable bbh got garbage value).

Last edited by sumeet inani; 10-28-2011 at 03:31 AM.
 
Old 10-29-2011, 12:03 PM   #2
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian Stable and Unstable
Posts: 1,943

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Ummm... You do know that this is forum is for Linux questions, don't you? Sorry, but I haven't used Windows in years, so I can't help you with batch files or anything else pertaining to Windows.
 
0 members found this post helpful.
Old 10-29-2011, 12:25 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by sgosnell View Post
Ummm... You do know that this is forum is for Linux questions, don't you? Sorry, but I haven't used Windows in years, so I can't help you with batch files or anything else pertaining to Windows.
Actually, this sub-forum is for "Non-*NIX" related questions, of which "Programming" falls under that domain.
 
Old 10-30-2011, 05:53 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Code:
C:\SUMEET\XYPLORER>ffprobe  "I:\video\PRESER~1\serial\absolute zero\Absolute Zer
o.flv" 1> g:\fa 2>&1
FFprobe version SVN-r23607, Copyright (c) 2007-2010 the FFmpeg developers
...
Argument '1' provided as input filename, but 'I:\video\PRESER~1\serial\absolute
zero\Absolute Zero.flv' was already specified.
It looks like the redirections weren't parsed correctly. Perhaps when called via system the interpreter starts in some old compatability mode. Maybe if you invoke it with cmd /C it would work.

Also, I would recommend sprintf instead of multiple strcats.
 
1 members found this post helpful.
Old 10-31-2011, 01:46 PM   #5
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908

Original Poster
Blog Entries: 26

Rep: Reputation: 49
thanks ntubski
(1)sprintf replaced my multiple strcat's

(2)system("cmd /c batch_file")
Played the file properly.

Now I feel , I was unnecessarily decoding media file for video height . Actually I found reserving 40 pixel at bottom with font size 3 is appropriate for any video file. This leads to command
Code:
k -subfont-text-scale 3 -fs -vf screenshot,expand=0:-40:0:0 "video_file" -sub "subtitle_file"
I had this idea of predictin subtitle file by matching number of similiar characters.
BUT
Turns out video file name is passed in 8.3 format to my program.
http://support.microsoft.com/kb/142982 gives rule of making short file name.

I think wiser method would be to give user a list of choices (when multiple subtitle file found).

Last edited by sumeet inani; 11-01-2011 at 03:41 AM.
 
Old 11-06-2011, 06:59 AM   #6
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908

Original Poster
Blog Entries: 26

Rep: Reputation: 49
Finally , here is my program which plays video file with subtitle (if only one srt file found) else prompts user from a list of likely candidates for subtitle file.
Code:
//printf("\nflag line 1"); used while debugging
#include<string.h> // for strcat,strcpy,strlen,memicmp,strrev
#include<stdio.h> // for fgets,fopen,fprintf,fclose
#include<stdlib.h> // for atoi,system
#include<conio.h> //for clrscr
//(3)currently we are using g:\log,list,a.bat files
char name[128],finalsf[128],sf[20][128],fp[128];//sf=subtitle file,name=only video file name
int sfnumber;//number of subtitle files found

int check(char d[128])
{	int len,cr;//cr=compare-result
	char bkup[128];
	len=strlen(d);
	d[len-1]=0;//because last character in string received is \n then 0
	strcpy(bkup,d);
	strrev(d);
	cr = memicmp(d, "trs", 3);
	if (cr==0)
	{	//printf("\n%d:%s is an srt file",len,bkup);
		strcpy(sf[sfnumber],bkup);
		return 1;
	}
	else
	{	//printf("\n%s!=trs",d);
		return 0;
	}
}

int findsubtitlefile()
{	FILE *fr,*log;//fr=read g:\\list.txt,log=write to g:\\log.txt
	char fn[128];//fn=file name
	log=fopen("g:\\log.txt","a");
	if(log==NULL)
		printf ("\nCould not append g:\\log.txt in function findsubtitlefile");
	fr=fopen("g:\\list.txt","r");
	if(fr==NULL)
		printf("\ncould not read g:\\list.txt");
	fprintf(log,"\nwe have to search srt in following \"name-length:file-name\" pairs \n");
	while ( fgets ( fn, 127, fr ) != NULL ) 
	{	fprintf(log,"%d:%s\n",strlen(fn),fn);
		if(check(fn))
		{	fprintf(log,"found subtitle file : %s\n",strrev(fn));
			sfnumber++;
		}
	}
	fclose(fr);
	fclose(log);
	if(sfnumber>0)
		return 1;
	else
		return 0;
}
void letuserdecide()
{	int i;
	printf("\nPLEASE CHOOSE SUBTITLE FILE");
	for(i=0;i<sfnumber;i++)
		printf("\n%d)%s",i+1,sf[i]);
	printf("\nEnter option:");
	scanf("%d",&i);
	strcpy(finalsf,sf[i-1]);
}

void main(int argc,char *argv[])
{	FILE *log,*action;
	int i,j,len;
	char dir[120],command[512];
	clrscr();
	log=fopen("g:\\log.txt","w");
	if(log==NULL)
		printf ("\nCould not open g:\\log.txt for writing ");
	// fp=full path 
	for(i=1;i<argc;i++)
	{
		strcat(fp,argv[i]);
		strcat(fp," ");//here sprintf will not work because we are appending to fp in each loop
	}
	fp[strlen(fp)-1]=0;
	fprintf(log,"full path=%s\nnumber of words=%d",fp,argc-1);
//copying directory name to 'dir' variable
	len=strlen(fp);
	i=strlen(fp)-1;
	while(fp[i]!='\\')
		i=i-1;
	for(j=0;j<i;j++)
		dir[j]=fp[j];
		dir[j]=0;
	fprintf(log,"\ndirectory is %s",dir);
//copying file name to 'name' variable
	for(j=0,i++;i<len;i++,j++)
		name[j]=fp[i];
	//printf("\nfile name:%s",name);
	fprintf(log,"\nfile name  : %s",name);
//executing 'dir' command to output all items in current folder to g:\list.txt
	sprintf(command,"ls -1 \"%s\" > g:\\list.txt",dir);
	system(command);
	fprintf(log,"\nwe have executed : %s",command);
	fclose(log);
	command[0]=0;
//let us find the subtitle file
	if (findsubtitlefile())
	{	//run the file with subtitle
			if(sfnumber>1)
			letuserdecide();
		else
				strcpy(finalsf,sf[0]);
		sprintf(command,"\nk -subfont-text-scale 3 -fs -vf screenshot,expand=0:-40:0:0 \"%s\" -sub \"%s\\%s\"",fp,dir,finalsf);
	}
	else
	{	//run file without subtitle
			sprintf(command,"\nk -fs \"%s\"",fp);
	}
	//printf("\nwe will run %s",command);
	action=fopen("g:\\a.bat","w");
	if(action==NULL)
		printf("\nCould not open g:\\a.bat to write kovensky play command");
	fputs(command,action);
	fclose(action);
	system("cmd /c g:\\a.bat");
	//getchar(); while debugging to read output before exiting
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program doesn't run properly on boot KenPem Programming 8 12-27-2007 04:42 PM
How to run a BASH script in a Batch file (with Cygwin) FaeDine Programming 2 10-27-2007 04:47 PM
how to write a batch file to make a program run during boot up in windows??? b0nd Programming 7 09-04-2006 06:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration