The simplest transcoding program based on FFmpeg [Turn]--analysis

Source: Internet
Author: User

Module:

Libavcodec-Codec
Libavdevice-Input support
Libavfilter-Video Audio filter Support
Libavformat-Visual Audio format parsing
Libavutil-Tools Library
Libpostproc-Post effect processing
Libswscale-Image Color, size conversion

1. Main function Analysis:

  1. Int_tmain (int argc, _tchar* argv[])
  2. {
  3. int ret;
  4. Avpacketpacket;
  5. Avframe *frame= NULL;
  6. enum Avmediatype type;
  7. unsigned intstream_index;
  8. unsigned int i;
  9. int got_frame;
  10. Int (*dec_func) (Avcodeccontext *, Avframe *, int *, const avpacket*);
  11. if (argc! = 3) {
  12. Av_log (NULL, Av_log_error, "Usage:%s<input file> <output file>\n", argv[0]);
  13. return 1;
  14. }
  15. Av_register_all ();
  16. Avfilter_register_all ();
  17. if (ret = Open_input_file (argv[1)) < 0)
  18. Goto END;
  19. if (ret = Open_output_file (argv[2)) < 0)
  20. Goto END;
  21. if (ret = Init_filters ()) < 0)
  22. Goto END;
  23. / * Read ALL packets * /
  24. While (1) {
  25. if ((ret= av_read_frame (Ifmt_ctx, &packet)) < 0)
  26. Break ;
  27. Stream_index = Packet.stream_index;
  28. Type =ifmt_ctx->streams[packet.stream_index]->codec->codec_type;
  29. Av_log (NULL, Av_log_debug, "Demuxergave frame of Stream_index%u\n",
  30. Stream_index);
  31. if (filter_ctx[stream_index].filter_graph) {
  32. Av_log (NULL, Av_log_debug, "Going toreencode&filter the frame\n");
  33. Frame =av_frame_alloc ();
  34. if (!frame) {
  35. ret = Averror (ENOMEM);
  36. Break ;
  37. }
  38. Packet.dts = Av_rescale_q_rnd (Packet.dts,
  39. Ifmt_ctx->streams[stream_index]->time_base,
  40. Ifmt_ctx->streams[stream_index]->codec->time_base,
  41. (avrounding) (av_round_near_inf| Av_round_pass_minmax));
  42. packet.pts = Av_rescale_q_rnd (packet.pts,
  43. Ifmt_ctx->streams[stream_index]->time_base,
  44. Ifmt_ctx->streams[stream_index]->codec->time_base,
  45. (avrounding) (av_round_near_inf| Av_round_pass_minmax));
  46. Dec_func = (Type = = Avmedia_type_video)? Avcodec_decode_video2:
  47. Avcodec_decode_audio4;
  48. RET =dec_func (Ifmt_ctx->streams[stream_index]->codec, Frame,
  49. &got_frame, &packet);
  50. if (Ret < 0) {
  51. Av_frame_free (&frame);
  52. Av_log (NULL, Av_log_error, "decodingfailed\n");
  53. Break ;
  54. }
  55. if (got_frame) {
  56. Frame->pts = Av_frame_get_best_effort_timestamp (frame);
  57. Ret= Filter_encode_write_frame (frame, stream_index);
  58. Av_frame_free (&frame);
  59. if (ret< 0)
  60. Goto END;
  61. } Else {
  62. Av_frame_free (&frame);
  63. }
  64. } Else {
  65. / * Remux this frame without reencoding * /
  66. Packet.dts = Av_rescale_q_rnd (Packet.dts,
  67. Ifmt_ctx->streams[stream_index]->time_base,
  68. Ofmt_ctx->streams[stream_index]->time_base,
  69. (avrounding) (av_round_near_inf| Av_round_pass_minmax));
  70. packet.pts = Av_rescale_q_rnd (packet.pts,
  71. Ifmt_ctx->streams[stream_index]->time_base,
  72. Ofmt_ctx->streams[stream_index]->time_base,
  73. (avrounding) (av_round_near_inf| Av_round_pass_minmax));
  74. RET =av_interleaved_write_frame (Ofmt_ctx, &packet);
  75. if (Ret < 0)
  76. Goto END;
  77. }
  78. Av_free_packet (&packet);
  79. }
  80. / * Flush Filters and encoders * /
  81. For (i = 0; i < ifmt_ctx->nb_streams; i++) {
  82. / * Flush filter * /
  83. if (!filter_ctx[i].filter_graph)
  84. continue;
  85. RET =filter_encode_write_frame (NULL, i);
  86. if (Ret < 0) {
  87. Av_log (NULL, Av_log_error, "Flushingfilter failed\n");
  88. Goto END;
  89. }
  90. / * Flush Encoder * /
  91. ret = Flush_encoder (i);
  92. if (Ret < 0) {
  93. Av_log (NULL, Av_log_error, "Flushingencoder failed\n");
  94. Goto END;
  95. }
  96. }
  97. Av_write_trailer (OFMT_CTX);
  98. End
  99. Av_free_packet (&packet);
  100. Av_frame_free (&frame);
  101. For (i = 0; i < ifmt_ctx->nb_streams; i++) {
  102. Avcodec_close (IFMT_CTX->STREAMS[I]->CODEC);
  103. if (ofmt_ctx && ofmt_ctx->nb_streams >i && ofmt_ctx->streams[i] &&ofmt_ctx-> STREAMS[I]->CODEC)
  104. Avcodec_close (OFMT_CTX->STREAMS[I]->CODEC);
  105. if (filter_ctx && filter_ctx[i].filter_graph)
  106. Avfilter_graph_free (&filter_ctx[i].filter_graph);
  107. }
  108. Av_free (FILTER_CTX);
  109. Avformat_close_input (&IFMT_CTX);
  110. if (Ofmt_ctx &&! ( Ofmt_ctx->oformat->flags & Avfmt_nofile))
  111. Avio_close (OFMT_CTX->PB);
  112. Avformat_free_context (OFMT_CTX);
  113. if (Ret < 0)
  114. Av_log (NULL, Av_log_error, "erroroccurred\n");
  115. return (ret. 1:0);
  116. }

1.1 int _tmain (int argc, _tchar* argv[])

People who have used C know that every C program will have a main (), but sometimes it is written by someone else to find that the main function is not an int main (), but an int _tmain (), and the header file is not <iostream.h> but <stdafx.h Will you be confused? First of all, this _tmain () is to support Unicode used by the main an alias, since it is an alias, there should be a macro defined, where is defined? In the <stdafx.h> that confused you, there are two lines
#include <stdio.h>
#include <tchar.h>
We can find the macro definition of _tmain in the header file <tchar.h>
#define _tmain Main
So, after pre-compiling, _tmain becomes main.

The _TCHAR type is a wide-character string, unlike the string we commonly use, which is a type used in 32-bit or higher operating systems.

The simplest transcoding program based on FFmpeg [Turn]--analysis

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.