/* * read_frame for video - this will try to read the next frame */ void CQTVideoByteStream::read_frame (uint32_t frame_to_read) { uint32_t next_frame_size; if (m_frame_in_buffer == frame_to_read) { #ifdef DEBUG_QTIME_VIDEO_FRAME player_debug_message("frame in buffer %u %u", m_byte_on, m_this_frame_size); #endif m_byte_on = 0; return; } // Haven't already read the next frame, so - get the size, see if // it fits, then read it into the appropriate buffer m_parent->lock_file_mutex(); next_frame_size = quicktime_frame_size(m_parent->get_file(), frame_to_read, m_track); if (next_frame_size > m_max_frame_size) { m_max_frame_size = next_frame_size + 4; m_buffer = (uint8_t *)realloc(m_buffer, (next_frame_size + 4) * sizeof(char)); } m_this_frame_size = next_frame_size; quicktime_set_video_position(m_parent->get_file(), frame_to_read, m_track); m_frame_in_buffer = frame_to_read; #ifdef DEBUG_QTIME_VIDEO_FRAME player_debug_message("reading into buffer %u", m_this_frame_size); #endif quicktime_read_frame(m_parent->get_file(), (unsigned char *)m_buffer, m_track); #ifdef DEBUG_QTIME_VIDEO_FRAME player_debug_message("Buffer %d %02x %02x %02x %02x", frame_to_read, m_buffer[0], m_buffer[1], m_buffer[2], m_buffer[3]); #endif m_parent->unlock_file_mutex(); m_byte_on = 0; }
/* * mp4extract * required arg1 should be the MP4 input file * required arg2 should be the output file for the extracted track */ int main(int argc, char** argv) { /* variables controlable from command line */ bool extractVideo = TRUE; /* internal variables */ char* mp4FileName = NULL; char* outFileName = NULL; quicktime_t* mp4File = NULL; FILE* outFile = NULL; int track = 0; int frame, numFrames; u_int8_t buf[64*1024]; /* big enough? */ int bufSize; /* begin process command line */ progName = argv[0]; while (1) { int c = -1; int option_index = 0; static struct option long_options[] = { { "audio", 0, 0, 'a' }, { "video", 0, 0, 'v' }, { NULL, 0, 0, 0 } }; c = getopt_long(argc, argv, "av", long_options, &option_index); if (c == -1) break; switch (c) { case 'a': { extractVideo = FALSE; break; } case 'v': { extractVideo = TRUE; break; } case '?': break; default: fprintf(stderr, "%s: unknown option specified, ignoring: %c\n", progName, c); } } /* check that we have at least two non-option arguments */ if ((argc - optind) < 2) { fprintf(stderr, "usage: %s <mpeg-file> <mov-file>\n", progName); exit(1); } /* point to the specified file names */ mp4FileName = argv[optind++]; outFileName = argv[optind++]; /* warn about extraneous non-option arguments */ if (optind < argc) { fprintf(stderr, "%s: unknown options specified, ignoring: ", progName); while (optind < argc) { fprintf(stderr, "%s ", argv[optind++]); } fprintf(stderr, "\n"); } /* end processing of command line */ /* open the MP4 file */ mp4File = quicktime_open(mp4FileName, 1, 0, 0); if (mp4File == NULL) { fprintf(stderr, "%s: error %s: %s\n", progName, mp4FileName, strerror(errno)); exit(2); } /* open output file for writing */ outFile = fopen(outFileName, "wb"); if (outFile == NULL) { fprintf(stderr, "%s: error opening %s: %s\n", progName, outFileName, strerror(errno)); exit(3); } /* add MPEG-4 video config info at beginning of output file */ if (extractVideo) { if (!strcasecmp(quicktime_video_compressor(mp4File, track), "mp4v")) { u_char* pConfig; int configSize; quicktime_get_mp4_video_decoder_config(mp4File, track, &pConfig, &configSize); if (fwrite(pConfig, 1, configSize, outFile) != configSize) { fprintf(stderr, "%s: write error: %s\n", progName, strerror(errno)); exit(4); } free(pConfig); } } if (extractVideo) { numFrames = quicktime_video_length(mp4File, track); } else { numFrames = quicktime_audio_length(mp4File, track); } for (frame = 0; frame < numFrames; frame++) { if (extractVideo) { bufSize = quicktime_read_frame(mp4File, &buf[0], track); } else { // extract audio bufSize = quicktime_read_audio_frame(mp4File, &buf[0], sizeof(buf), track); } if (bufSize <= 0) { fprintf(stderr, "%s: read error: %s\n", progName, strerror(errno)); exit(5); } if (fwrite(buf, 1, bufSize, outFile) != bufSize) { fprintf(stderr, "%s: write error: %s\n", progName, strerror(errno)); exit(4); } } /* cleanup */ quicktime_close(mp4File); fclose(outFile); exit(0); }