int CSVPToolBox::HandleSubPackage(FILE* fp){ //Extract Package printf( "Extracting package\n" ); char szSBuff[8]; if ( fread(szSBuff , sizeof(char), 4, fp) < 4){ printf("Fail to retrive Package Data Length\n" ); return -1; } Char4ToInt(szSBuff); if ( fread(szSBuff , sizeof(char), 4, fp) < 4){ printf("Fail to retrive Desc Data Length\n"); return -2; } size_t iDescLength = this->Char4ToInt(szSBuff); if(iDescLength > 0){ char * szDescData = this->ReadToPTCharByLength(fp, iDescLength); if(!szDescData){ return -4; } // convert szDescData to MutliByte and save to WideChar szaSubDescs.push_back(MutliByte2WideChar(szDescData)); free(szDescData); }else{ //szaSubDescs.Add(_T("")); szaSubDescs.push_back(L""); } this->ExtractSubFiles(fp); return 0; }
int main(int argc, char* argv[]) { if (argc!=2) { printf("Usage:GetSubtitle videopath\n"); return 0; } printf("Please wait...\n"); setlocale(LC_ALL,""); SubTransFormat SubTF; char* strfilepath=argv[1]; std::wstring filepath=MutliByte2WideChar(strfilepath); std::wstring filehash=SubTF.ComputerFileHash_STL(strfilepath); std::wstring shortname= SubTF.GetShortFileNameForSearch(filepath); char* strfilehash=WideChar2MultiByte(filehash.c_str()); char* strshortname=WideChar2MultiByte(shortname.c_str()); std::wstring uniqueIDHash2=SubTF.genVHash(strfilepath,strfilehash); char* struniqueIDHash2=WideChar2MultiByte(uniqueIDHash2.c_str()); printf("hash:%s\nshortname:%s\nuniqueIDHash:%s\n",strfilehash,strshortname,struniqueIDHash2); struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; char* form; form=WideChar2MultiByte(filepath.c_str()); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "pathinfo", CURLFORM_COPYCONTENTS, form,CURLFORM_END); free(form); form=WideChar2MultiByte(filehash.c_str()); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filehash", CURLFORM_COPYCONTENTS, form,CURLFORM_END); free(form); form=WideChar2MultiByte(uniqueIDHash2.c_str()); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "vhash", CURLFORM_COPYCONTENTS, form,CURLFORM_END); free(form); form=WideChar2MultiByte(shortname.c_str()); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "shortname", CURLFORM_COPYCONTENTS, form,CURLFORM_END); free(form); CURL* curl = curl_easy_init(); SetCURLopt(curl); char url[100]; char filename[L_tmpnam]; tmpnam(filename); FILE* fp=fopen(filename,"wb+"); //FILE* fp=tmpfile(); if(fp==NULL) return 0; printf("Searching for subtitles\n\n"); for (int i=1;i<=7;i++) { GetUrlByType('sapi',i,url); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); CURLcode res = curl_easy_perform(curl); if (res==0) { int respcode=0; curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &respcode); if (respcode==200) { ExtractDataFromAiSubRecvBuffer_STL(fp); } break; } else { long l_oserr = 0; curl_easy_getinfo(curl,CURLINFO_OS_ERRNO,&l_oserr); printf(curl_easy_strerror(res)); } } curl_easy_cleanup(curl); std::wstring szSubFilePath; for (int i = 0; i < toolbox.szaSubTmpFileList.size(); i++) { szSubFilePath = toolbox.getSubFileByTempid_STL(i,strfilepath); if (szSubFilePath == L"EXIST") printf("\nThe same subtitle already existed!\n"); else if (!szSubFilePath.empty()) printf("\nSubtitle downloaded to %s\n\n",WideChar2MultiByte(szSubFilePath.c_str())); else printf("\nFail to get sub file\n"); } return 0; }
std::wstring CSVPToolBox::getSubFileByTempid_STL(int iTmpID,char* strPath) { //get base path name std::wstring szVidPath=MutliByte2WideChar(strPath); std::vector<std::wstring> szVidPathInfo; std::wstring szTargetBaseName = L""; std::wstring szDefaultSubPath = L""; std::wstring StoreDir = L""; getVideoFileBasename(szVidPath, &szVidPathInfo); StoreDir = szVidPathInfo.at(SVPATH_DIRNAME).c_str(); std::wstring tmBasenamePath=StoreDir; getVideoFileBasename(szVidPath, &szVidPathInfo); tmBasenamePath+=(szVidPathInfo.at(SVPATH_FILENAME).c_str()); std::wstring szBasename = tmBasenamePath; //set new file name std::vector<std::wstring> szSubfiles; //std::wstring szXTmpdata = this->szaSubTmpFileList.GetAt(iTmpID); std::wstring szXTmpdata = this->szaSubTmpFileList.at(iTmpID); Explode(szXTmpdata, L";", &szSubfiles); bool bIsIdxSub = false; int ialreadyExist = 0; if (szSubfiles.size() < 1) printf("Not enough files in tmp array"); for(int i = 0; i < szSubfiles.size(); i++) { std::vector<std::wstring> szSubTmpDetail; Explode(szSubfiles[i], L"|", &szSubTmpDetail); if (szSubTmpDetail.size() < 2) { printf("Not enough detail in sub tmp string"); continue; } std::wstring szSource = szSubTmpDetail[1]; std::wstring szLangExt = L".chn"; //TODO: use correct language perm if(bIsIdxSub) szLangExt = L""; else szLangExt = DetectSubFileLanguage_STL(szSource);//check if english sub if (szSubTmpDetail[0].at(0) != L'.') szSubTmpDetail[0] = L"." + szSubTmpDetail[0]; std::wstring szTarget = szBasename + szLangExt + szSubTmpDetail[0]; szTargetBaseName = szBasename + szLangExt ; CMD5Checksum cm5source; std::wstring szSourceMD5 = cm5source.GetMD5(szSource); std::wstring szTargetMD5; //check if target exist wchar_t szTmp[128]; wcscpy(szTmp, L""); int ilan = 1; while(ifFileExist_STL(szTarget)) { //TODO: compare if its the same file cm5source.Clean(); szTargetMD5 = cm5source.GetMD5(szTarget); if(szTargetMD5 == szSourceMD5) { // TODO: if there is a diffrence in delay //printf("同样的字幕文件已经存在了\n"); ialreadyExist++; //TODO: 如果idx+sub里面只有一个文件相同怎么办 ??~~ break; } swprintf(szTmp,128, L"%d", ilan); szTarget = szBasename + szLangExt + (std::wstring)szTmp + szSubTmpDetail[0]; szTargetBaseName = szBasename + szLangExt + (std::wstring)szTmp; ilan++; } printf("Copying file\n"); CopyFile(szSource.c_str(), szTarget.c_str()); if (((bIsIdxSub && szSubTmpDetail[0].compare(L"idx") == 0) || !bIsIdxSub) && szDefaultSubPath.empty()) szDefaultSubPath = szTarget; std::vector<std::wstring> szaDesclines; if (szaSubDescs.size() > iTmpID) { Explode(szaSubDescs.at(iTmpID), L"\x0b\x0b", &szaDesclines); if(szaDesclines.size() > 0) { int iDelay = 0; swscanf(szaDesclines.at(0).c_str(), L"delay=%d", &iDelay); if (iDelay) { wchar_t szBuf[128]; swprintf(szBuf, 128, L"%d", iDelay); filePutContent_STL(szTarget + L".delay", (std::wstring)szBuf); } else remove(WideChar2MultiByte((szTarget + L".delay").c_str())); } } else printf("Count of szaSubDescs not match with count of subs "); unlink(WideChar2MultiByte(szSource.c_str())); } if(ialreadyExist) return L"EXIST"; else return szDefaultSubPath; }
int CSVPToolBox::ExtractEachSubFile(FILE* fp, int iSubPosId){ // get file ext name printf( "Extracting eachfile\n" ); char szSBuff[4096]; if ( fread(szSBuff , sizeof(char), 4, fp) < 4){ printf("Fail to retrive Single File Pack Length"); return -1; } if ( fread(szSBuff , sizeof(char), 4, fp) < 4){ return -1; } size_t iExtLength = this->Char4ToInt(szSBuff); char* szExtName = this->ReadToPTCharByLength(fp, iExtLength); if(!szExtName){ return -2; } //get filedata length if ( fread(szSBuff , sizeof(char), 4, fp) < 4){ return -1; } size_t iFileLength = this->Char4ToInt(szSBuff); // gen tmp name and tmp file point char otmpfilename[L_tmpnam]; tmpnam(otmpfilename); FILE* fpt=fopen(otmpfilename,"wb+"); //unlink(otmpfilename);can not unlink here,the file will be opened again. if(fpt==NULL){ return -4; } // copy data to tmp file size_t leftoread = iFileLength; do{ size_t needtoread = SVP_MIN( 4096, leftoread ); size_t accturead = fread(szSBuff , sizeof(char), needtoread, fp); if(accturead == 0){ //wtf break; } leftoread -= accturead; fwrite(szSBuff, sizeof(char), accturead , fpt);//把szSBuff里的东西写到otmpfilename里 }while(leftoread > 0); fclose( fpt ); char otmpfilenameraw[L_tmpnam]; tmpnam(otmpfilenameraw); printf("\nDecompressing file\n"); unpackGZfile( otmpfilename , otmpfilenameraw ); //unlink(otmpfilenameraw);can not unlink here,the file will be opened again. // add filename and tmp name to szaTmpFileNames //this->szaSubTmpFileList[iSubPosId].Append( this->UTF8ToCString(szExtName, iExtLength)); //why cant use + ??? this->szaSubTmpFileList[iSubPosId].append(MutliByte2WideChar(szExtName)); this->szaSubTmpFileList[iSubPosId].append(L"|" ); this->szaSubTmpFileList[iSubPosId].append(MutliByte2WideChar(otmpfilenameraw)); this->szaSubTmpFileList[iSubPosId].append( L";"); unlink(otmpfilename); return 0; }