j4ggi

MicroDVD Subtitles' FPS Converter

Jul 28th, 2013
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. /* ---------------------------------------------------------------------------
  2. ** This software is in the public domain, furnished "as is", without technical
  3. ** support, and with no warranty, express or implied, as to its usefulness for
  4. ** any purpose.
  5. **
  6. ** MicroDVD Subtitles' FPS Converter.cpp
  7. ** This file contains the very whole code. Developed using the Dev-C++ IDE.
  8. **
  9. ** Author: jaggi
  10. ** Version: 1.0.0.0
  11. ** contact: [email protected]
  12. ** -------------------------------------------------------------------------*/
  13.  
  14. #include <iostream>
  15. #include <conio.h>
  16. #include <fstream>
  17. #include <string>
  18. #include <cmath>
  19. #include <cstdlib>
  20. #include <sstream>
  21.  
  22. using namespace std;
  23.  
  24. void convert(int& frame, float fps_s, float fps_d)  { frame=ceil(frame*fps_d/fps_s); }
  25. template <typename T> string tostr(const T& t) { ostringstream os; os<<t; return os.str(); }
  26.  
  27. int count(const char* name)
  28. {
  29.     string temp;
  30.     int x=0;
  31.     ifstream file1(name);
  32.     while(getline(file1, temp)) x++;
  33.     x++;
  34.     file1.close();
  35.     return x;
  36. }
  37.  
  38. float menu(string msg)
  39. {
  40.     cout<<msg<<"\n1. 12 \n2. 15 \n3. 18\n4. 20\n5. 23.976 \n6. 24 \n7. 25 \n8. 29.97 \n9. 30\n";
  41.     char choice=getch();
  42.     switch(choice)
  43.     {
  44.         case '1':return 12;
  45.         case '2':return 15;
  46.         case '3':return 18;
  47.         case '4':return 20;
  48.         case '5':return 23.976;
  49.         case '6':return 24;
  50.         case '7':return 25;
  51.         case '8':return 29.97;
  52.         case '9':return 30;
  53.         default: return 0;
  54.     }
  55. }
  56.  
  57. void modify(string& line, float fps_s, float fps_d)
  58. {
  59.     string sf1, sf2, temp="";
  60.     int f1,f2. i=0;
  61.     for(;i<line.length();i++)
  62.     {
  63.         if(line[i]=='{')
  64.         {
  65.             i++;
  66.             while (line[i]!='}')
  67.             {
  68.                   temp+=line[i];
  69.                   i++;
  70.             }
  71.             break;
  72.         }
  73.     }
  74.     f1=atoi(temp.c_str());
  75.     temp="";
  76.     for(;i<line.length();i++)
  77.     {
  78.         if(line[i]=='{')
  79.         {
  80.             i++;
  81.             while (line[i]!='}')
  82.             {
  83.                   temp+=line[i];
  84.                   i++;
  85.             }
  86.             break;
  87.         }
  88.     }
  89.     f2=atoi(temp.c_str());
  90.     convert(f1, fps_s, fps_d);
  91.     convert(f2, fps_s, fps_d);
  92.     sf1=tostr(f1);
  93.     sf2=tostr(f2);
  94.     line="{"+sf1+"}"+"{"+sf2+"}"+line.erase(0, i+1);
  95. }
  96.  
  97. void file(const char* name, int x)
  98. {
  99.     ifstream file1(name);  
  100.     string tmp;
  101.     string* tab=new string[x];
  102.     for (int i=0;i<=x;i++)
  103.         getline(file1, tab[i]);
  104.    
  105.     float fps_s, fps_d;
  106.     fps_s=menu("Source file FPS");
  107.     fps_d=menu("Destination file FPS");
  108.     for (int i=0;i<x;i++)
  109.         modify(tab[i], fps_s, fps_d);
  110.     tmp=name;
  111.     tmp=tmp.insert(tmp.length()-4, "_");
  112.     tab[0]="{1}{1}";
  113.     tab[0]+=tostr(fps_d);
  114.     ofstream file2(tmp.c_str());
  115.     for (int i=0;i<x;i++)
  116.         file2<<tab[i]<<endl;
  117.     file1.close();
  118.     file2.close();
  119. }
  120.  
  121. int main(int argc, char** argv)
  122. {
  123.     cout<<"MicroDVD Subtitles' FPS Converter by jaggi.\nNo rights reserved :)\nEnjoy ;)\n";
  124.     cout<<"contact ==> jaggi"<<"@"<<"interia.eu\n\n";
  125.     if(argc==1)
  126.     {
  127.         cout<<"!Note!\nYou may drag your subtitles & drop on the converter's icon \nto skip typing filename.\n\n";
  128.         cout<<"Enter filename (with extension). Be sure it is spelled correctly.\n";
  129.         string name;
  130.         cin>>name;
  131.         int x=count(name.c_str());
  132.         file(name.c_str(), x);
  133.         cout<<"\nDone :)\nOutput file is named "<<name.insert(name.length()-4, "_")<<".\nPress any key to exit.";
  134.         getch();
  135.         return 0;
  136.     }
  137.     int x=count(argv[1]);
  138.     file(argv[1], x);
  139.     string name=argv[1];
  140.     cout<<"\nDone :)\nOutput file is named "<<name.insert(name.length()-4, "_")<<".\nPress any key to exit.";
  141.     getch();
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment