目標:從內存加載電影數據,然後寫入到電影文件的Mac。從內存加載電影數據,然後寫入電影文件
要做到這一點,我發現這一點: https://developer.apple.com/legacy/library/technotes/tn1195/_index.html
我的代碼部分是:
void createMovieFromMemory(void *data,
Size dataSize,
Str255 fileName)
{
Handle myDataRef = NULL;
MovieImportComponent miComponent;
Track targetTrack = nil;
TimeValue addedDuration = 0;
long outFlags = 0;
OSErr err;
ComponentResult result;
Movie movie = NULL;
miComponent = OpenDefaultComponent(MovieImportType,kQTFileTypeMP4); //kQTFileTypeWave
//implementation of this is learned from the link given above
myDataRef = createPointerDataRefWithExtensions(data, /* pointer to data */
dataSize, /* size of data */
fileName, /* file name */
0, /* file type */
nil); /* mime type */
if (myDataRef)
{
OSErr err = noErr;
short id = 0;
err = MovieImportDataRef(miComponent,
myDataRef,
HandleDataHandlerSubType,
movie,
nil,
&targetTrack,
nil,
&addedDuration,
movieImportCreateTrack,
&outFlags);
if (err == noErr)
DisposeMovie(movie);
DisposeHandle(myDataRef);
}
}
現在我的問題是什麼是在內存加載了影片的正確方法(用於測試目的),我應該如何安排電影數據在內存中。
目前,我使用下面的代碼加載電影記憶:
unsigned int getFileSize(FILE **file){
unsigned int size;
if(fseek(*file, 0, SEEK_END) == -1){ return -1; }
size = ftell(*file);
fseek(*file, 0, SEEK_SET);
return size;
}
char *getFileBuffer(FILE **file, unsigned int fileSize){
char *buffer = malloc(fileSize + 1);
fread(buffer, fileSize, 1, *file);
return buffer;
}
unsigned int readFileToMemory(char path[], char **buffer){
unsigned int fileSize;
FILE *file = fopen(path, "rb");
if(file != NULL){
fileSize = getFileSize(&file);
*buffer = getFileBuffer(&file, fileSize);
fclose(file);
return fileSize;
}else{
*buffer = NULL;
return -1;
}
}
我累了它與下面的代碼:
int main(int argc, const char* argv[])
{
void* pData = NULL;
Size iDataSize = 0;
Str255 strFileName="/Users/x/SAMPLE_convert.mp4";
char path[] = "/Users/x/SAMPLE.AVI";
char *buffer;
int fileSize;
fileSize = readFileToMemory(path, &buffer);
iDataSize = fileSize;
createMovieFromMemory((void*)buffer,iDataSize,strFileName);
return (0);
}
功能MovieImportDataRef()返回錯誤代碼-50。我周圍搜索無法找到錯誤代碼-50。任何建議,歡迎。謝謝。
JL