⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp4creator.cpp

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is MPEG4IP.
 * 
 * The Initial Developer of the Original Code is Cisco Systems Inc.
 * Portions created by Cisco Systems Inc. are
 * Copyright (C) Cisco Systems Inc. 2001-2002.  All Rights Reserved.
 * 
 * Contributor(s): 
 *		Dave Mackie		dmackie@cisco.com
 */

#define MP4CREATOR_GLOBALS
#include "mp4creator.h"
#include "mpeg4ip_getopt.h"

// forward declarations

MP4TrackId* CreateMediaTracks(
	MP4FileHandle mp4File, 
	const char* inputFileName);

void CreateHintTrack(
	MP4FileHandle mp4File, 
	MP4TrackId mediaTrackId,
	const char* payloadName, 
	bool interleave, 
	u_int16_t maxPayloadSize);

void ExtractTrack(
	MP4FileHandle mp4File, 
	MP4TrackId trackId, 
	const char* outputFileName);

// external declarations

// track creators
MP4TrackId* AviCreator(
	MP4FileHandle mp4File, const char* aviFileName);

MP4TrackId AacCreator(
	MP4FileHandle mp4File, FILE* inFile);

MP4TrackId Mp3Creator(
	MP4FileHandle mp4File, FILE* inFile);

MP4TrackId Mp4vCreator(
	MP4FileHandle mp4File, FILE* inFile);


// main routine
int main(int argc, char** argv)
{
	char* usageString = 
		"usage: %s <options> <mp4-file>\n"
		"  Options:\n"
		"  -create=<input-file>    Create track from <input-file>\n"
		"    input files can be of type: .aac .mp3 .divx .mp4v .m4v .cmp .xvid\n"
		"  -extract=<track-id>     Extract a track\n"
		"  -delete=<track-id>      Delete a track\n"
		"  -hint[=<track-id>]      Create hint track, also -H\n"
		"  -interleave             Use interleaved audio payload format, also -I\n"
		"  -list                   List tracks in mp4 file\n"
		"  -mtu=<size>             MTU for hint track\n"
		"  -optimize               Optimize mp4 file layout\n"
	        "  -payload=<payload>      Rtp payload type \n"
                "                          (use 3119 or mpa-robust for mp3 rfc 3119 support)\n"
		"  -rate=<fps>             Video frame rate, e.g. 30 or 29.97\n"
		"  -timescale=<ticks>      Time scale (ticks per second)\n"
		"  -verbose[=[1-5]]        Enable debug messages\n"
        "  -version                Display version information\n"
		;

	bool doCreate = false;
	bool doExtract = false;
	bool doDelete = false;
	bool doHint = false;
	bool doList = false;
	bool doOptimize = false;
	bool doInterleave = false;
	char* mp4FileName = NULL;
	char* inputFileName = NULL;
	char* outputFileName = NULL;
	char* payloadName = NULL;
	MP4TrackId hintTrackId = MP4_INVALID_TRACK_ID;
	MP4TrackId extractTrackId = MP4_INVALID_TRACK_ID;
	MP4TrackId deleteTrackId = MP4_INVALID_TRACK_ID;
	u_int16_t maxPayloadSize = 1460;

	Verbosity = MP4_DETAILS_ERROR;
	VideoFrameRate = 0;		// determine from input file
	Mp4TimeScale = 90000;

	// begin processing command line
	ProgName = argv[0];

	while (true) {
		int c = -1;
		int option_index = 0;
		static struct option long_options[] = {
			{ "create", 1, 0, 'c' },
			{ "delete", 1, 0, 'd' },
			{ "extract", 1, 0, 'e' },
			{ "help", 0, 0, '?' },
			{ "hint", 2, 0, 'H' },
			{ "interleave", 0, 0, 'I' },
			{ "list", 0, 0, 'l' },
			{ "mtu", 1, 0, 'm' },
			{ "optimize", 0, 0, 'O' },
			{ "payload", 1, 0, 'p' },
			{ "rate", 1, 0, 'r' },
			{ "timescale", 1, 0, 't' },
			{ "verbose", 2, 0, 'v' },
			{ "version", 0, 0, 'V' },
			{ NULL, 0, 0, 0 }
		};

		c = getopt_long_only(argc, argv, "c:d:e:H::Ilm:Op:r:t:v::V",
			long_options, &option_index);

		if (c == -1)
			break;

		switch (c) {
		case 'c':
			doCreate = true;
			inputFileName = optarg;
			break;
		case 'd':
			if (sscanf(optarg, "%u", &deleteTrackId) != 1) {
				fprintf(stderr, 
					"%s: bad track-id specified: %s\n",
					 ProgName, optarg);
				exit(EXIT_COMMAND_LINE);
			}
			doDelete = true;
			break;
		case 'e':
			if (sscanf(optarg, "%u", &extractTrackId) != 1) {
				fprintf(stderr, 
					"%s: bad track-id specified: %s\n",
					 ProgName, optarg);
				exit(EXIT_COMMAND_LINE);
			}
			doExtract = true;
			break;
		case 'H':
			doHint = true;
			if (optarg) {
				if (sscanf(optarg, "%u", &hintTrackId) != 1) {
					fprintf(stderr, 
						"%s: bad track-id specified: %s\n",
						 ProgName, optarg);
					exit(EXIT_COMMAND_LINE);
				}
			}
			break;
		case 'I':
			doInterleave = true;
			break;
		case 'l':
			doList = true;
			break;
		case 'm':
			u_int32_t mtu;
			if (sscanf(optarg, "%u", &mtu) != 1 || mtu < 64) {
				fprintf(stderr, 
					"%s: bad mtu specified: %s\n",
					 ProgName, optarg);
				exit(EXIT_COMMAND_LINE);
			}
			maxPayloadSize = mtu - 40;	// subtract IP, UDP, and RTP hdrs
			break;
		case 'O':
			doOptimize = true;
			break;
		case 'p':
			payloadName = optarg;
			break;
		case 'r':
			if (sscanf(optarg, "%f", &VideoFrameRate) != 1) {
				fprintf(stderr, 
					"%s: bad rate specified: %s\n",
					 ProgName, optarg);
				exit(EXIT_COMMAND_LINE);
			}
			break;
		case 't':
			if (sscanf(optarg, "%u", &Mp4TimeScale) != 1) {
				fprintf(stderr, 
					"%s: bad timescale specified: %s\n",
					 ProgName, optarg);
				exit(EXIT_COMMAND_LINE);
			}
			break;
		case 'v':
			Verbosity |= (MP4_DETAILS_READ | MP4_DETAILS_WRITE);
			if (optarg) {
				u_int32_t level;
				if (sscanf(optarg, "%u", &level) == 1) {
					if (level >= 2) {
						Verbosity |= MP4_DETAILS_TABLE;
					} 
					if (level >= 3) {
						Verbosity |= MP4_DETAILS_SAMPLE;
					} 
					if (level >= 4) {
						Verbosity |= MP4_DETAILS_HINT;
					} 
					if (level >= 5) {
						Verbosity = MP4_DETAILS_ALL;
					} 
				}
			}
			break;
		case '?':
			fprintf(stderr, usageString, ProgName);
			exit(EXIT_SUCCESS);
		case 'V':
		  fprintf(stderr, "%s - %s version %s\n", 
			  ProgName, PACKAGE, VERSION);
		  exit(EXIT_SUCCESS);
		default:
			fprintf(stderr, "%s: unknown option specified, ignoring: %c\n", 
				ProgName, c);
		}
	}

	// check that we have at least one non-option argument
	if ((argc - optind) < 1) {
		fprintf(stderr, usageString, ProgName);
		exit(EXIT_COMMAND_LINE);
	}

	if ((argc - optind) == 1) {
		mp4FileName = argv[optind++];
	} else {
		// it appears we have two file names
		if (doExtract) {
			mp4FileName = argv[optind++];
			outputFileName = argv[optind++];
		} else {
			if (inputFileName == NULL) {
				// then assume -c for the first file name
				doCreate = true;
				inputFileName = argv[optind++];
			}
			mp4FileName = 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");
	}

	// operations consistency checks

	if (!doList && !doCreate && !doHint 
	  && !doOptimize && !doExtract && !doDelete) {
		fprintf(stderr, 
			"%s: no operation specified\n",
			 ProgName);
		exit(EXIT_COMMAND_LINE);
	}
	if ((doCreate || doHint) && doExtract) {
		fprintf(stderr, 
			"%s: extract operation must be done separately\n",
			 ProgName);
		exit(EXIT_COMMAND_LINE);
	}
	if ((doCreate || doHint) && doDelete) {
		fprintf(stderr, 
			"%s: delete operation must be done separately\n",
			 ProgName);
		exit(EXIT_COMMAND_LINE);
	}
	if (doExtract && doDelete) {
		fprintf(stderr, 
			"%s: extract and delete operations must be done separately\n",
			 ProgName);
		exit(EXIT_COMMAND_LINE);
	}

	// end processing of command line

	if (doList) {
		// just want the track listing
		char* info = MP4FileInfo(mp4FileName);

		if (!info) {
			fprintf(stderr, 
				"%s: can't open %s\n", 
				ProgName, mp4FileName);
			exit(EXIT_INFO);
		}

		fputs(info, stdout);
		free(info);
		exit(EXIT_SUCCESS);
	}

	// test if mp4 file exists
	bool mp4FileExists = (access(mp4FileName, F_OK) == 0);

	MP4FileHandle mp4File;

	if (doCreate || doHint) {
		if (!mp4FileExists) {
			if (doCreate) {
				mp4File = MP4Create(mp4FileName, Verbosity);
				if (mp4File) {
					MP4SetTimeScale(mp4File, Mp4TimeScale);
				}
			} else {
				fprintf(stderr,
					"%s: can't hint track in file that doesn't exist\n", 
					ProgName);
				exit(EXIT_CREATE_FILE);
			}
		} else {
			mp4File = MP4Modify(mp4FileName, Verbosity);
		}

		if (!mp4File) {
			// mp4 library should have printed a message
			exit(EXIT_CREATE_FILE);
		}

		bool allMpeg4Streams = true;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -