Four, the main function code
001 int Main (int argc, char **argv)
002 {
003 const char *filename;
004 Avoutputformat *FMT;
005 Avformatcontext *oc;
006 Avstream *audio_st, *video_st;
007 double audio_pts, video_pts;
008 int i;
/* Initialize LIBAVCODEC, and register all codecs and formats */
009 Av_register_all ();
010 if (argc! = 2) {
011 printf ("Usage:%s output_file\n")
"API example program to output a media file with libavformat.\n"
"The output format is automatically guessed according to the file extension.\n"
"Raw images can also is output by using '%%d ' in the filename\n"
"\ n", argv[0]);
012 exit (1);
}
013 filename = argv[1];
/* Auto detect the output format from the name. Default is MPEG. */
014 FMT = Guess_format (null, filename, null);
015 if (!FMT) {
016 printf ("Could not deduce output format from file extension:using mpeg.\n");
017 FMT = Guess_format ("MPEG", NULL, NULL);
018}
019 if (!FMT) {
fprintf (stderr, "Could not find suitable output format\n");
Exit (1);
}
/* Add the audio and video streams using the default format codecs
and initialize the codecs * *
027 Video_st = NULL;
028 Audio_st = NULL;
029 if (Fmt->video_codec! = Codec_id_none) {
030 Video_st = Add_video_stream (OC, FMT->VIDEO_CODEC);
031}
032 if (fmt->audio_codec! = Codec_id_none) {
033 Audio_st = Add_audio_stream (OC, FMT->AUDIO_CODEC);
034}
/* Set the output parameters (must is done even if no parameters). */
035 if (av_set_parameters (OC, NULL) < 0) {
036 fprintf (stderr, "Invalid output Format parameters\n");
037 exit (1);
038}
039 dump_format (OC, 0, filename, 1);
/* Now is the all the parameters is set, we can open the audio and
Video codecs and allocate the necessary encode buffers * *
040 if (Video_st)
041 open_video (OC, Video_st);
042 if (audio_st)
043 Open_audio (OC, Audio_st);
/* Open the output file, if needed */
044 if (! ( Fmt->flags & Avfmt_nofile)) {
045 if (Url_fopen (&OC->PB, filename, url_wronly) < 0) {
046 fprintf (stderr, "Could not open '%s ' \ n", filename);
047 exit (1);
048}
049}
/* Write the stream header, if any */
050 Av_write_header (OC);
051 for (;;) {
/* Compute current audio and video time */
052 if (Audio_st)
053 audio_pts = (double) audio_st->pts.val * audio_st->time_base.num/audio_st->time_base.den;
054 Else
055 audio_pts = 0.0;
/* Write the trailer, if any. The trailer must be written
* Before you close the codeccontexts open if you wrote the
* HEADER; Otherwise write_trailer may try to use memory that
* Was freed on av_codec_close () */
069 Av_write_trailer (OC);
/* Close each codec */
070 if (Video_st)
071 Close_video (OC, Video_st);
072 if (audio_st)
073 Close_audio (OC, Audio_st);
/* Free the streams * *
074 for (i = 0; i < oc->nb_streams; i++) {
075 Av_freep (&OC->STREAMS[I]->CODEC);
076 Av_freep (&oc->streams[i]);
077}
078 if (! ( Fmt->flags & Avfmt_nofile)) {
079/* Close the output file */
080 Url_fclose (OC->PB);
081}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.