/**[txh]********************************************************************

  Copyright (c) 2002 Salvador Eduardo Tropea.
  This file is covered by the GPL license.
  Este archivo esta protegido por la licencia GPL.

  #include <disclaimer.h>
  For use with material you have the rights to manipulate, avoid using this
program with encripted and/or copyrighted DVDs. Read mencoder documentation.

***************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>

// DVD Chapter, usually when you send to create a DVD you get the video in
// the chapter 1.
const int dvdChapter=1;
// This is only if you used profesional filmic cameras (i.e. 35 or 16 mm)
const int fpsOut=24;

// Recommended audio options
const int audioQuality=4,
         
audioAlgoQuality=1;

// Output size, that's for a 650 Mb CD where you reserve some room for
// extra information and CD TOC.
const int aviSize=644;

int movieLen;

int LookForMovieLen(const char *file)
{
 
FILE *f=fopen(file,"rt");
 
if (!f) return 0;
 
char b[200];
 
while (!feof(f))
   {
   
fgets(b,199,f);
   
if (strncmp(b,"Video stream",12)==0)
      {
      
float vbr,len;
      
int size,bps,frames;
      
if (sscanf(b,"Video stream:    %f kbit/s  (%d bps)  size: %d bytes  %f secs  %d frames",&vbr,&bps,&size,&len,&frames)!=5)
         
return 0;
      
//printf("%s\n%f %d %d %f %d\n",b,vbr,bps,size,len,frames);
      
movieLen=(int)(len+0.9);
      
int h,m,s,l;
      
l=movieLen;
      
h=l/3600; l-=h*3600;
      
m=l/60;   l-=m*60;
      
s=l;
      
printf("Movie length: %d:%02d:%02d aprox. (%d s)\n",h,m,s,movieLen);
      
return 1;
      }      
   }
 
return 0;
}

int main(int argc, char *argv[])
{
 
// Remove temporal files
 
unlink("frameno.avi");
 
unlink("test.avi");
 
unlink("subtitles.idx");
 
unlink("subtitles.sub");
 
unlink("lavc_stats.txt");

 
char b[PATH_MAX];
 
sprintf(b,"mencoder dvd://%d -ofps %d -ovc frameno -oac mp3lame -lameopts aq=%d:q=%d -aid 128 -o frameno.avi -slang es -vobsubout subtitles -vobsuboutindex 0 -vobsuboutid es > mplayer_out.txt",
        
dvdChapter,fpsOut,audioAlgoQuality,audioQuality);
 
system(b);
 
//printf("%s\n",b);

 
struct stat st;
 
if (stat("frameno.avi",&st)!=0)
   {
   
printf("Error stating frameno.avi, operation aborted\n");
   
return 1;
   }
 
printf("Trying to generate a file of %d Mb\n",aviSize);
 
printf("Audio size: %6.2f Mb\n",st.st_size/(double)(1<<20));
 
unsigned sizeAvail=(aviSize<<20)-st.st_size;
 
printf("Space available for the video: %6.2f Mb\n",sizeAvail/(double)(1<<20));

 
if (!LookForMovieLen("mplayer_out.txt"))
   {
   
printf("Unable to determine movie length :-(\n");
   
return 2;
   }

 
int videoBR=(int)(sizeAvail*8.0/movieLen/1000.0);
 
printf("Video bit rate: %d Kb/s\n",videoBR);

 
int i;
 
for (i=1; i<=2; i++)
    {
    
sprintf(b,"mencoder dvd://%d -ofps %d -vop scale=640:360 -sws 2 -ovc lavc -lavcopts vcodec=mpeg4:vpass=%d:vhq:keyint=250:vbitrate=%d -oac copy -o movie.avi >> mplayer_out.txt",
            
dvdChapter,fpsOut,i,videoBR);
    
system(b);
    
//printf("%s\n",b);
   
}
 
system("ls -lah");
 
return 0;
}