iTranscode.cpp

Go to the documentation of this file.
00001 // iTranscode.cpp : Defines the entry point for the console application.
00002 //
00003 
00004 #include "stdafx.h"
00005 #include <dshow.h>
00006 #include <iostream>
00007 #include <windows.h>
00008 using namespace std;
00009 
00010 
00011 
00012 bool transcodeToVorbis(string inFileName) {
00013         IGraphBuilder* locGraphBuilder = NULL;
00014         IMediaControl* locMediaControl = NULL;
00015         HRESULT locHR;
00016         CoInitialize(NULL);
00017         locHR = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&locGraphBuilder);
00018         locHR = locGraphBuilder->RenderFile(StringHelper::toWStr(inFileName).c_str(), NULL);
00019 
00020         if (locHR != S_OK) {
00021                 //Release memory here !!
00022                 if (locGraphBuilder != NULL) {
00023                         locGraphBuilder->Release();
00024                 }
00025                 CoUninitialize();
00026                 return false;
00027         }
00028 
00029         locHR = locGraphBuilder->QueryInterface(IID_IMediaControl, (void**)&locMediaControl);
00030         
00031         IMediaEvent* locMediaEvent = NULL;
00032         locHR = locGraphBuilder->QueryInterface(IID_IMediaEvent, (void**)&locMediaEvent);
00033 
00034         IEnumFilters* locFilterEnum = NULL;
00035         locHR = locGraphBuilder->EnumFilters(&locFilterEnum);
00036 
00037         bool locStillMore = true;
00038 
00039         IBaseFilter* locFilter = NULL;
00040         
00041         locHR = S_OK;
00042         ULONG locHowManyFilters = 0;
00043         ULONG locHowManyPins = 0;
00044         while (locHR == S_OK) {
00045                 //Loop through all the filter.
00046                 locHR = locFilterEnum->Next(1, &locFilter, &locHowMany);
00047                 if (locHR == S_OK) {
00048                         //When we find one... loop through it's pins.
00049                         HRESULT locPinHR = S_OK;
00050                         IEnumPins* locPinEnum = NULL;
00051                         IPin* locPin = NULL;
00052                         PIN_DIRECTION locPinDirn;
00053                         bool locHasOutputs = false;
00054                         locPinHR = locFilter->EnumPins(&locPinEnum);
00055                         if (locPinHR == S_OK) {
00056                                 while (locPinHR == S_OK) {
00057                                         //Loop through the pins.
00058                                         locPinHR = locPinEnum->Next(1, &locPin, &locHowManyPins);
00059                                         if (locPinHR == S_OK) {
00060                                                 locPin->QueryDirection(&locPinDirn);
00061                                                 if (locPinDirn == PINDIR_OUTPUT) {
00062                                                         //Has an output pin... can't be a renderer, break out of the pin loop.
00063                                                         locHasOutputs = true;
00064                                                         break;
00065                                                 }
00066 
00067                                         }
00068                                 }
00069 
00070                                 if (!locHasOutputs) {
00071                                         //Renderer filter here. Remove it from the graph.
00072                                         locHR = locGraphBuilder->RemoveFilter(locFilter);
00073                                         
00074                                         //Break out of the filter loop.
00075                                         break;
00076                                 }
00077 
00078                         }
00079 
00080                 }
00081         }
00082 
00083 
00084         IBaseFilter* locVorbisEncoder = NULL;
00085         locHR = CoCreateInstance(CLSID_VorbisEncodeFilter, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&locVorbisEncoder);
00086 
00087         if (locHR == S_OK) {
00088                 //Created a vorbis filter...
00089                 locGraphBuilder->AddFilter(locVorbisEncoder, L"Vorbis Encode Filter");
00090 
00091                 
00093 
00094         }
00095 
00096 
00097         locHR = locMediaControl->Run();
00098 
00099         HANDLE  hEvent; 
00100         long    evCode, param1, param2;
00101         BOOLEAN bDone = FALSE;
00102         HRESULT hr = S_OK;
00103         hr = locMediaEvent->GetEventHandle((OAEVENT*)&hEvent);
00104         if (FAILED(hr))
00105         {
00106             /* Insert failure-handling code here. */
00107         }
00108         while(!bDone) 
00109         {
00110             if (WAIT_OBJECT_0 == WaitForSingleObject(hEvent, 100))
00111             { 
00112                         while (hr = locMediaEvent->GetEvent(&evCode, &param1, &param2, 0), SUCCEEDED(hr)) 
00113                         {
00114                     //printf("Event code: %#04x\n Params: %d, %d\n", evCode, param1, param2);
00115                                 //cout<<"Event : "<<evCode<<" Params : "<<param1<<", "<<param2<<endl;
00116                                 locMediaEvent->FreeEventParams(evCode, param1, param2);
00117                                 bDone = (EC_COMPLETE == evCode);
00118                         }
00119                 }
00120         } 
00121 
00122         cout<<"Finished..."<<endl;
00123         int x;
00124         cin>>x;
00125         locMediaControl->Release();
00126         locGraphBuilder->Release();
00127         CoUninitialize();
00128 
00129         return 0;
00130 
00131         return true;
00132 }
00133 int _tmain(int argc, _TCHAR* argv[])
00134 {
00135         if (argc < 3) {
00136                 cout<<"Usage       : iTranscode <source path/file mask> <target directory>"<<endl;
00137                 cout<<"              target directory must end with a backslash"<<endl;
00138                 cout<<"Description : This program will search the directory for any files you specify and"<<endl;
00139                 cout<<"              attempt to transcode them to ogg vorbis"<<endl;
00140         } else {
00141                 unsigned long locNumFound = 0;  
00142 
00143 
00144                 WIN32_FIND_DATA locFindData;
00145                 HANDLE locFindHandle;
00146 
00147                 
00148                 locFindHandle = FindFirstFile(argv[1], &locFindData); //, FindExSearchNameMatch, NULL, 0 );
00149 
00150                 if (locFindHandle == INVALID_HANDLE_VALUE) {
00151                         cout<<"Invalid file mask, or no matches."<<endl;
00152                         
00153                 } else {
00154                         
00155                         if ((locFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
00156                                 locNumFound++;
00157                                 cout<<"Found "<<locNumFound << " : "<< locFindData.cFileName<<endl;
00158                                 string locFileName = locFindData.cFileName;
00159                                 bool locTransOK = transcodeToVorbis(locFileName);
00160                         }
00161                 
00162                         BOOL locFindOK = TRUE;
00163                         while (locFindOK == TRUE) {
00164                                 locFindOK = FindNextFile(locFindHandle, &locFindData);
00165                                 if (locFindOK == TRUE) {
00166                                         if ((locFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
00167                                                 locNumFound++;
00168                                                 cout<<"Found "<<locNumFound << " : "<< locFindData.cFileName<<endl;;
00169                                                 bool locTransOK = transcodeToVorbis(locFileName);
00170                                         }
00171                                 }
00172         
00173                         }
00174                         FindClose(locFindHandle);
00175                 }
00176         }
00177 
00178 
00179         return 0;
00180 }
00181 

Generated on Tue Feb 15 14:54:23 2005 for oggdsf by  doxygen 1.3.9