MP4v2
example/callbacks/callbacks.c
/* This example makes use of the MP4IOCallbacks API to use custom input/output
* routines.
*/
#include <mp4v2/mp4v2.h>
#include <stdio.h>
/*****************************************************************************/
static int my_seek( void* handle, int64_t pos )
{
if( fseeko( (FILE*)handle, pos, SEEK_SET ) != 0)
return -1;
return 0;
}
static int my_read( void* handle, void* buffer, int64_t size, int64_t* nin )
{
if( fread( buffer, size, 1, (FILE*)handle ) != 1)
return -1;
*nin = size;
return 0;
}
static int64_t my_size( void* handle )
{
int64_t pos = ftello( (FILE*)handle );
if( fseeko( (FILE*)handle, 0, SEEK_END ) != 0 )
return -1;
int64_t size = ftello( (FILE*)handle );
if( fseeko( (FILE*)handle, pos, SEEK_SET ) != 0 )
return -1;
return size;
}
/*****************************************************************************/
int main( int argc, char** argv )
{
if( argc != 2 ) {
printf( "usage: %s file.mp4\n", argv[0] );
return 1;
}
/* populate data structure with custom functions.
* safe to put on stack as it will be immediately copied internally.
*/
MP4IOCallbacks callbacks = { 0 };
callbacks.seek = my_seek;
callbacks.read = my_read;
callbacks.size = my_size;
/* open file for read */
FILE* handle = fopen( argv[1], "rb" );
if( handle == NULL ) {
printf( "fopen failed\n" );
return 1;
}
MP4FileHandle file = MP4ReadCallbacks( &callbacks, handle );
if( file == MP4_INVALID_FILE_HANDLE ) {
printf( "MP4ReadCallbacks failed\n" );
return 1;
}
/* dump file contents */
if( !MP4Dump( file, 0 ))
printf( "MP4Dump failed\n" );
/* cleanup and close */
MP4Close( file, 0 );
fclose( handle );
return 0;
}
MP4Close
void MP4Close(MP4FileHandle hFile, uint32_t flags=0)
Close an mp4 file.
MP4ReadCallbacks
MP4FileHandle MP4ReadCallbacks(const MP4IOCallbacks *callbacks, void *handle=NULL)
Read an existing mp4 file using an I/O callbacks structure.
MP4_INVALID_FILE_HANDLE
#define MP4_INVALID_FILE_HANDLE
Constant: invalid MP4FileHandle.
Definition: general.h:48
MP4IOCallbacks_s
Structure of functions implementing custom I/O callbacks.
Definition: file.h:59
MP4Dump
bool MP4Dump(MP4FileHandle hFile, bool dumpImplicits=0)
Dump mp4 file contents as ASCII either to stdout or the log callback (see MP4SetLogCallback())