FFmpeg SDK Use instance output_example analysis of the second

Source: Internet
Author: User
Tags printf
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);
}


/* Allocate the output media context */
020 oc = Avformat_alloc_context ();
021 if (!OC) {
022 fprintf (stderr, "Memory error\n");
023 exit (1);
024}
025 Oc->oformat = FMT;
026 snprintf (oc->filename, sizeof (Oc->filename), "%s", filename);

/* 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 (&AMP;OC-&GT;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;

056 if (video_st)
057 video_pts = (double) video_st->pts.val * video_st->time_base.num/video_st->time_base.den;
058 Else
059 video_pts = 0.0;

060 if ((!audio_st | | audio_pts >= stream_duration) &&
061 (!video_st | | video_pts >= stream_duration))
062 break;

/* Write interleaved audio and video frames */
063 if (!video_st | | (Video_st && audio_st && audio_pts < video_pts)) {
064 write_audio_frame (OC, Audio_st);
065} else {
066 Write_video_frame (OC, Video_st);
067}
068}

/* 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 (&AMP;OC-&GT;STREAMS[I]-&GT;CODEC);
076 Av_freep (&oc->streams[i]);
077}

078 if (! ( Fmt->flags & Avfmt_nofile)) {
079/* Close the output file */
080 Url_fclose (OC-&GT;PB);
081}

082/* Free the stream * *
083 Av_free (OC);

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.