j4ggi

SubRip@MicroDVD Subtitles' Converter

Aug 17th, 2013
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 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. ** SubRip 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.2.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,unsigned long long ms)   { frame=ceil( ms/(1000/fps) ); }
  25.  
  26. template <typename T> string tostr(const T& t) { ostringstream os; os<<t; return os.str(); }
  27.  
  28. void modify(string& line, unsigned long long ms, unsigned long long ms2, float fps)
  29. {
  30.     string frame1, frame2;
  31.     int frame;
  32.     convert(frame, fps, ms);
  33.     frame1=tostr(frame);
  34.     convert(frame, fps, ms2);
  35.     frame2=tostr(frame);
  36.     line="{"+frame1+"}"+"{"+frame2+"}"+line;
  37. }
  38.  
  39. float menu(string msg)
  40. {
  41.     cout<<msg<<"\n1. 12 \n2. 15 \n3. 18\n4. 20\n5. 23.976 \n6. 24 \n7. 25 \n8. 29.97 \n9. 30\n";
  42.     char choice=getch();
  43.     switch(choice)
  44.     {
  45.         case '1':return 12;
  46.         case '2':return 15;
  47.         case '3':return 18;
  48.         case '4':return 20;
  49.         case '5':return 23.976;
  50.         case '6':return 24;
  51.         case '7':return 25;
  52.         case '8':return 29.97;
  53.         case '9':return 30;
  54.         default: return 0;
  55.     }
  56. }
  57.  
  58. int count(const char* name)
  59. {
  60.     string temp;
  61.     int x=0;
  62.     ifstream file1(name);
  63.     while(getline(file1, temp)) x++;
  64.     file1.close();
  65.     return x;
  66. }
  67.  
  68. void mainFunction(string* tab, int size, const char* name)
  69. {
  70.     int var=-1, x=0;
  71.     static unsigned int z=1;
  72.     unsigned long long ms, ms2;
  73.     float fps=menu("Movie FPS");
  74.     string tmp;
  75.    
  76.     for(int i=0;i<size;i++)
  77.             if( !tab[i].empty() && (tab[i][0]<'0' || tab[i][0]>'9') ) x++;
  78.     string* sArray=new string[x];
  79.     for(int i=0;i<size;i++)
  80.     {
  81.         int found=tab[i].find("-->");
  82.         if(found!=string::npos)
  83.         {
  84.                 int found2=tab[i].find(":");
  85.                 ms=atoi( (tab[i].substr(found2-2, 2)).c_str() )*3600000;
  86.                 ms+=atoi( (tab[i].substr(found2+1, 2)).c_str() )*60000;
  87.                 ms+=atoi( (tab[i].substr(found2+4, 2)).c_str() )*1000;
  88.                 ms+=atoi( (tab[i].substr(found2+7, 3)).c_str() );
  89.                 int found3=tab[i].rfind(":");
  90.                 ms2=atoi( (tab[i].substr(found3-5, 2)).c_str() )*3600000;
  91.                 ms2+=atoi( (tab[i].substr(found3-2, 2)).c_str() )*60000;
  92.                 ms2+=atoi( (tab[i].substr(found3+1, 2)).c_str() )*1000;
  93.                 ms2+=atoi( (tab[i].substr(found3+4, 3)).c_str() );
  94.                 var=1;
  95.                 continue;
  96.         }
  97.         if( tab[i].empty() ) { var=0; continue; }
  98.         if(var==1) 
  99.         {
  100.             if( tmp.empty() ) tmp=tab[i];
  101.             else tmp=tmp+"|"+tab[i];
  102.             if( (i+1)==size )
  103.             {
  104.                 sArray[z]=tmp;
  105.                 modify(sArray[z], ms, ms2, fps);
  106.                 break;
  107.             }
  108.             continue;
  109.         }
  110.         if(var==0)
  111.         {
  112.             sArray[z]=tmp;
  113.             modify(sArray[z], ms, ms2, fps);
  114.             z++;
  115.             tmp.clear();
  116.         }
  117.     }
  118.     sArray[0]="{1}{1}"+tostr(fps);
  119.     string label=name;
  120.     label=label.erase(label.length()-4, 4)+"_.txt";
  121.     ofstream file(label.c_str());
  122.     for(int i=0;i<=z;i++)
  123.         file<<sArray[i]<<endl;
  124.     file.close();
  125. }
  126.  
  127. int main(int argc, char** argv)
  128. {
  129.     cout<<"SubRip Subtitles' Converter by jaggi.\nNo rights reserved :)\nEnjoy ;)\n";
  130.     cout<<"contact ==> jaggi"<<"@"<<"interia.eu\n\n";
  131.     if(argc==1)
  132.     {
  133.         cout<<"!Note!\nYou may drag your subtitles & drop on the converter's icon \nto skip typing filename.\n\n";
  134.         cout<<"Enter filename (with extension). Be sure it is spelled correctly.\n";
  135.         string name;
  136.         cin>>name;
  137.         int x=count(name.c_str());
  138.         ifstream file(name.c_str());
  139.         string* tab=new string[x];
  140.         for(int i=0;i<x;i++)
  141.             getline(file, tab[i]);
  142.         mainFunction(tab, x, name.c_str());
  143.         cout<<"\nDone :)\nOutput file is named "<<name.erase(name.length()-4, 4)<<"_.txt"<<".\nPress any key to exit.";
  144.         getch();
  145.         return 0;
  146.     }
  147.     int x=count(argv[1]);
  148.     string name=argv[1];
  149.     ifstream file(argv[1]);
  150.     string* tab=new string[x];
  151.     for(int i=0;i<x;i++)
  152.         getline(file, tab[i]);
  153.     mainFunction(tab, x, argv[1]);
  154.     cout<<"\nDone :)\nOutput file is named "<<name.erase(name.length()-4, 4)<<"_.txt"<<".\nPress any key to exit.";
  155.     getch();
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment