The simplest FFMPEG + SDL-based audio player ver2 (using SDL2.0) and ffmpegsdl2.0

Source: Internet
Author: User
Tags svn client tortoisesvn

The simplest FFMPEG + SDL-based audio player ver2 (using SDL2.0) and ffmpegsdl2.0
Introduction

I have previously worked on a simple audio player: FFMPEG + SDL-based audio player, SDL1.2. Just two days ago, I updated the original "the simplest Video Player Based on FFMPEG + SDL" with SDL2.0, so I also updated the audio player to SDL2.0.

SourceForge project home: https://sourceforge.net/projects/simplestffmpegaudioplayer/

Complete: http://download.csdn.net/detail/leixiaohua1020/7850021


It should be noted that, unlike playing a video, the function of playing audio in SDL2.0 is slightly different from SDL1.2. Basically, it remains unchanged.


In addition to SDL2.0, the following changes are made:

* The project is rebuilt, unnecessary code is deleted, and the code is modified to be more standardized and easy to understand.

* You can use macros to control whether to Use SDL and whether to output PCM.

* Supports multiple formats, such as MP3 and AAC.


Source code

/*** The Simplest FFmpeg-Based Audio Player 2 (SDL 2.0) * Simplest FFmpeg Audio Player 2 (SDL 2.0) ** this version replaces SDL 2.0 in the first version with SDL 1.0. * Note: The audio decoding API in SDL 2.0 does not change. The only difference is that * the Audio Buffer in the callback function is not fully initialized and needs to be manually initialized. * In This example, SDL_memset (stream, 0, len); ** This version use SDL 2.0 instead of SDL 1.2 in version 1 * Note: The good news for audio is that, with one exception, * it's entirely backwards compatible with 1.2. * That one really important exception: The audio callback * does NOT start with a fully initialized buffer anymore. * You must fully write to the buffer in all cases. in this * example it is SDL_me Mset (stream, 0, len ); ** leixiao Lei Xiaohua * leixiaohua1020@126.com * China Media University/Digital TV Technology * Communication University of China/Digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this program implements audio decoding and playback. ** This software decode and play audio streams. ** Version 2.0 */# include "stdafx. h "# include <stdio. h> # include <stdlib. h> # include <string. h> extern "C" {# include "libavcodec/avcodec. h "# include" libavformat/avformat. h "# include" libswresample/swresample. h "// SDL # include" sdl/SDL. h "# include" sdl/SDL_thread.h "}; # define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48 khz 32bit audio // Output PCM # define OUTP UT_PCM 0 // Use SDL # define USE_SDL 1 // Buffer: // | ----------- | ------------- | // chunk ------- pos --- len ----- | static Uint8 * audio_chunk; static Uint32 audio_len; static Uint8 * audio_pos;/* The audio function callback takes the following parameters: * stream: A pointer to the audio buffer to be filled * len: The length (in bytes) of the audio buffer * callback function */void fill_audio (void * udata, Uint8 * stream, int le N) {// SDL 2.0SDL _ memset (stream, 0, len); if (audio_len = 0)/* Only play if we have data left */return; len = (len> audio_len? Audio_len: len);/* Mix as much data as possible */SDL_MixAudio (stream, audio_pos, len, SDL_MIX_MAXVOLUME); audio_pos + = len; audio_len-= len ;} // ----------------- int _ tmain (int argc, _ TCHAR * argv []) {AVFormatContext * pFormatCtx; inti, audioStream; AVCodecContext * pCodecCtx; AVCodec * pCodec; char url [] = "WavinFlag. aac "; av_register_all (); // supports Network Stream Input avformat_network_init (); // initializes pFormatCtx = avformat_alloc_context (); // Open if (avformat_open_input (& pFormatCtx, url, NULL, NULL )! = 0) {printf ("Couldn't open input stream. \ n "); return-1;} // Retrieve stream informationif (av_find_stream_info (pFormatCtx) <0) {printf (" Couldn't find stream information. \ n "); return-1;} // Dump valid information onto standard errorav_dump_format (pFormatCtx, 0, url, false); // Find the first audio streamaudioStream =-1; for (I = 0; I <pFormatCtx-> nb_streams; I ++) if (pFormatCtx-> streams [I]-> codec-> codec_type = AV MEDIA_TYPE_AUDIO) {audioStream = I; break;} if (audioStream =-1) {printf ("Didn't find a audio stream. \ n "); return-1;} // Get a pointer to the codec context for the audio streampCodecCtx = pFormatCtx-> streams [audioStream]-> codec; // Find the decoder for the audio streampCodec = avcodec_find_decoder (pCodecCtx-> codec_id); if (pCodec = NULL) {printf ("Codec not found. \ n "); return-1;} // Open codecif (avcodec_open2 (pCodec Ctx, pCodec, NULL) <0) {printf ("cocould not open codec. \ n "); return-1;} FILE * pFile = NULL; # if OUTPUT_PCMpFile = fopen (" output. pcm "," wb "); # endifAVPacket * packet = (AVPacket *) malloc (sizeof (AVPacket); bytes (packet); // Out Audio Paramuint64_t out_channel_layout = bytes; int out_nb_samples = 1024; AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16; int out_sample_rate = 44100; int out_channels = av_get_c Outputs (out_channel_layout); // output memory size int out_buffer_size = bytes (NULL, out_channels, channels, out_sample_fmt, 1); uint8_t * out_buffer = (uint8_t *) av_malloc (Bytes * 2); AVFrame * pFrame; pFrame = avcodec_alloc_frame (); // SDL ---------------- # if USE_SDL // Initif (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | audio )) {printf ("cocould not initialize SD L-% s \ n ", SDL_GetError (); return-1;} // define wanted_spec; bandwidth = out_sample_rate; wanted_spec.format = AUDIO_S16SYS; bandwidth = out_channels; bandwidth = 0; outputs = out_nb_samples; wanted_spec.callback = fill_audio; wanted_spec.userdata = pCodecCtx; if (SDL_OpenAudio (& wanted_spec, NULL) <0) {printf ("can't open audio. \ n "); return -1 ;}# endifprintf ("Bitrate: \ t % 3d \ n", pFormatCtx-> bit_rate); printf ("Decoder Name: \ t % s \ n ", pCodecCtx-> codec-> long_name); printf ("Channels: \ t % d \ n", pCodecCtx-> channels ); printf ("Sample per Second \ t % d \ n", pCodecCtx-> sample_rate); uint32_t ret, len = 0; int got_picture; int index = 0; struct SwrContext * au_convert_ctx; au_convert_ctx = swr_alloc (); au_convert_ctx = swr_alloc_set_opts (au_convert_ctx, out_chan Nel_layout, metrics, metrics, pCodecCtx-> channel_layout, pCodecCtx-> metrics, pCodecCtx-> sample_rate, 0, NULL); swr_init (au_convert_ctx); while (convert (pFormatCtx, packet)> = 0) {if (packet-> stream_index = audioStream) {ret = avcodec_decode_audio4 (pCodecCtx, pFrame, & got_picture, packet); if (ret <0) {printf ("Error in decoding audio frame. \ n "); return-1;} if (got_picture> 0) {sw R_convert (au_convert_ctx, & out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t **) pFrame-> data, pFrame-> nb_samples); # if 1 printf ("index: % 5d \ t pts: % 10d \ t packet size: % d \ n ", index, packet-> pts, packet-> size); # endif # if OUTPUT_PCM // Write PCMfwrite (out_buffer, 1, out_linesize, pFile); # endif // FIX: FLAC, MP3, AAC Different number of samplesif (wanted_spec.samples! = PFrame-> nb_samples) {SDL_CloseAudio (); out_nb_samples = pFrame-> nb_samples; out_buffer_size = bytes (NULL, out_channels, channels, buffers, 1); bytes = bytes; SDL_OpenAudio (& wanted_spec, NULL);} index ++;} // SDL ---------------- # if USE_SDL // set audio data buffer, PCM Data audio_chunk = (Uint8 *) out_buffer; // set the Audio Data Length audio_len = out_buffer_size; audio_pos = audio_chunk; // The number of audio records to be played back. SDL_PauseAudio (0); while (audio_len> 0) // wait until the audio data is played! SDL_Delay (1); # endif} av_free_packet (packet);} swr_free (& au_convert_ctx); # if USE_SDLSDL_CloseAudio (); // disable the audio device SDL_Quit (); # endif // Close filefclose (pFile); av_free (out_buffer); // Close the codecavcodec_close (pCodecCtx); // Close the video fileav_close_input_file (pFormatCtx); return 0 ;}




When compiling ffmpeg, the SDL support no information appears in the compilation information. How can this problem be solved?

Cygwin or MinGw?
MinGW I Just configured
Download sdl-devel-1.2.14-mingw32.tar.gz and put the lib bin include in the corresponding MiGW folder.
Modify SDL-config to prefix = c:/MinGW.
 
Add subtitles to the video compiled by ffmpeg

Compile and use ffmpeg

1 ffmpeg Introduction

Ffmpeg is a complete solution for audio/video separation, conversion, encoding and decoding, and streaming media. The most important solution is the libavcodec library. It is used as a decoder by mplayer or xine. In addition, the mainstream player audio and video storm in China or mypc's backend ffdshow also uses the ffmpeg decoding library.

After compilation, the ffmpeg package generates three executable files: ffmpeg, ffserver, and ffplay. Ffmpeg is used to process media files, ffserver is an http Streaming Media Server, and ffplay is a simple SDL-based player.

Ffmpeg contains five library files: libavcodec, libavformat, libavutil, libswscale, and libpostproc. The library libavcodec and libavformat are used to process media files, such as format conversion; libavutil is a common small function library that generates a CRC Check Code. It is a 128-bit integer mathematics, a maximum common number, an integer square, an integer logarithm, and memory allocation, big-end, small-end format conversion and other functions; libswscale and libpostproc do not know what to use.

2 ffmpeg download

The latest ffmpeg can be downloaded through svn. SVN-Assisted Software includes:

SubVersion, Which is downloaded from subversion.tigris.org/and supports linux.

TortoiseSVN, downloaded from tortoisesvn.tigris.org/, is a good SVN client program. It is easy to use by integrating windows Shell programs into windows Resource Manager and file management system Subversion clients.

Subversion installation. Remember to install apr and apr-util before.

Wget subversion.tigris.org/downloads/subversion-1.3.2.tar.gz

Tar zvxf subversion-1.3.2.tar.gz

Cd subversion-1.3.2

./Configure -- with-apr =/usr/local/apr-httpd -- with-apr-util =/usr/local/apr-util-httpd/

Make

Make install

If FC6 is installed, it has svn. You do not need to install FC6.

Download ffmpeg: You can use the svn command to obtain the latest ffmpeg. The command is as follows:

Svn checkout svn: // svn. mplayerhq. hu/ffmpeg/trunk ffmpeg

3 ffmpeg support library Installation

* Xvid

The xvid retrieval address is as follows:

... The remaining full text>

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.