FFmpeg SDK Development & use C/C ++ to develop a vlc sdk-Based Video Player

Source: Internet
Author: User

//

Http://blog.csdn.net/smilestone_322/article/details/9275029

 

From: http://blog.sina.com.cn/s/blog_62a8419a01016exv.html

FFmpeg SDK Development

 

FFmpeg (4)-audio decoding and encoding

 

Http://blog.csdn.net/yang_xian521/article/details/7699620

The audio and video are actually the same. You can find the audio stream in the file and decompress it to obtain the audio frame data. You can also compress the data according to the specified encoding format, here I have made audio decoding and encoding into two projects, and I am also directly using the code:

# Include <stdio. h> # include <stdlib. h> extern "C" {# include <libavcodec \ avcodec. h> # include <libavformat \ avformat. h> # include <libswscale \ swscale. h >}int main (char Arg, char * argv []) {char * filename = argv [1]; av_register_all (); // register all decoded avformatcontext * pinfmtctx = NULL; // the file format avcodeccontext * pincodecctx = NULL; // The encoding format if (av_open_input_file (& pinfmtctx, filename, null, 0, null )! = 0) // obtain the file format printf ("av_open_input_file error \ n"); If (av_find_stream_info (pinfmtctx) <0) // obtain the information of audio and video streams in the file printf ("av_find_stream_info error \ n"); unsigned Int J; // find the first audio streamint audiostream =-1; for (j = 0; j <pinfmtctx-> nb_streams; j ++) // find the stream corresponding to the audio {If (pinfmtctx-> streams [J]-> codec-> codec_type = codec_type_audio) {audiostream = J; break ;}} if (audiostream =-1) {printf ("input file has no Au Dio stream \ n "); Return 0; // didn't find a audio stream} printf (" audio stream num: % d \ n ", audiostream ); pincodecctx = pinfmtctx-> streams [audiostream]-> codec; // Audio Encoding context avcodec * pincodec = NULL; pincodec = avcodec_find_decoder (pincodecctx-> codec_id ); // locate the decoded struct Based on the encoding ID if (pincodec = NULL) {printf ("error no codec found \ n"); Return-1; // codec not found} If (avcodec_open (pincodecctx, pincodec) <0) // combine the two The following decoding function calls the corresponding decoding function {printf ("error avcodec_open failed. \ n "); Return-1; // cocould not open codec} static avpacket packet; printf (" bit_rate = % d \ r \ n ", pincodecctx-> bit_rate ); printf ("sample_rate = % d \ r \ n", pincodecctx-> sample_rate); printf ("channels = % d \ r \ n", pincodecctx-> channels ); printf ("code_name = % s \ r \ n", pincodecctx-> codec-> name); printf ("block_align = % d \ n", pincodecctx-> block_align); Uint8_t * pktdata; int pktsize; int out_size = Limit * 100; uint8_t * inbuf = (uint8_t *) malloc (out_size); file * PCM; PCM = fopen ("result. PCM "," WB "); long start = clock (); While (av_read_frame (pinfmtctx, & Packet)> = 0) // call the packet function in the corresponding format in pinfmtctx to obtain the function {If (packet. stream_index = audiostream) // If the audio is {pktdata = packet. data; pktsize = packet. size; while (pktsize> 0) {out_size = avcodec_max_audio_fram E_size * 100; // decode int Len = avcodec_decode_audio2 (pincodecctx, (short *) inbuf, & out_size, pktdata, pktsize); If (LEN <0) {printf ("error while decoding. \ n "); break;} If (out_size> 0) {fwrite (inbuf, 1, out_size, PCM); // PCM record fflush (PCM );} pktsize-= Len; pktdata + = Len ;}} av_free_packet (& Packet);} long end = clock (); printf ("cost time: % F \ n ", double (end-Start)/(double) clocks_per_sec); free (inbuf); fclose (PCM); If (pinc Odecctx! = NULL) {avcodec_close (pincodecctx);} av_close_input_file (pinfmtctx); Return 0 ;}

The decoded file is saved as result. PCM. Now we use this file to compress the new audio file. The Code is as follows:

Void main () {int16_t * samples; uint8_t * audio_outbuf; int audio_outbuf_size; int bytes; double audio_pts; const char * filename = "test.wav"; file * fin = fopen ("result. PCM "," rb "); // audio source file avoutputformat * FMT; avformatcontext * OC; avstream * audio_st; av_register_all (); FMt = guess_format (null, filename, null ); OC = av_alloc_format_context (); oc-> oformat = FMT; snprintf (oc-> filename, sizeof (oc-> F Ilename), "% s", filename); audio_st = NULL; If (FMT-> audio_codec! = Codec_id_none) {avcodeccontext * C; audio_st = av_new_stream (OC, 1); C = audio_st-> codec; C-> codec_id = FMT-> audio_codec; c-> codec_type = codec_type_audio; C-> bit_rate = 128000; C-> sample_rate = 44100; C-> channels = 2;} If (av_set_parameters (OC, null) <0) {return;} dump_format (OC, 0, filename, 1); If (audio_st) {avcodeccontext * C; avcodec * codec; C = audio_st-> codec; codec = avcodec_find_encoder (c-> codec_id); AVC Odec_open (C, codec); audio_outbuf_size = 10000; audio_outbuf = (uint8_t *) av_malloc (audio_outbuf_size); If (c-> frame_size <= 1) {audio_input_frame_size = audio_outbuf_size/C-> channels; Switch (audio_st-> codec-> codec_id) {case when: audio_input_frame_size> = 1; break; default: break;} else {audio_input_frame_size = C-> fram E_size;} samples = (int16_t *) av_malloc (audio_input_frame_size * 2 * C-> channels);} If (! FMT-> flags & avfmt_nofile) {If (url_fopen (& oc-> Pb, filename, url_wronly) <0) {return ;}} av_write_header (OC); (;;) {If (audio_st) {audio_pts = (double) audio_st-> PTS. val * audio_st-> time_base.num/audio_st-> time_base.den;} else {audio_pts = 0.0;} If (! Audio_st | audio_pts >=360.0) {break;} If (fread (samples, 1, audio_input_frame_size * 2 * audio_st-> codec-> channels, fin) <= 0) {break;} avcodeccontext * C; avpacket Pkt; av_init_packet (& Pkt); C = audio_st-> codec; Pkt. size = avcodec_encode_audio (C, audio_outbuf, audio_outbuf_size, samples); Pkt. PTS = av_rescale_q (c-> coded_frame-> pts, C-> time_base, audio_st-> time_base); Pkt. flags | = pkt_flag_key; Pkt. stream_index = Audio_st-> index; Pkt. Data = audio_outbuf; If (av_write_frame (OC, & Pkt )! = 0) {return ;}}if (audio_st) {avcodec_close (audio_st-> codec); av_free (samples); av_free (audio_outbuf);} av_write_trailer (OC ); for (INT I = 0; I <oc-> nb_streams; I ++) {av_freep (& oc-> streams [I]-> codec ); av_freep (& oc-> streams [I]);} If (! (FMT-> flags & avfmt_nofile) {url_fclose (oc-> Pb);} av_free (OC); fclose (FIN );}

Download link:

Audio Decoding: http://download.csdn.net/detail/yang_xian521/4399219

Audio Encoding: http://download.csdn.net/detail/yang_xian521/4399293

So far, I have implemented video decoding and audio decoding and encoding. I believe that some people will be able to achieve synchronous compression of video and audio on this basis, but I need to read some online materials, it is also very easy. As for video display and playback, most of them should be implemented in combination with SDL, because I only have a taste of it, only the image display and simple video playback are realized, I recommend a few links that are weak, so I won't write them down. This series is here. I hope to help some entry-level players like me. You are also welcome to give me more advice.

Develop a vlc sdk-based video player using C/C ++

Http://blog.sina.com.cn/s/blog_62a8419a01014eey.html

In Windows, If you develop a universal player, it is generally developed based on DirectShow. The development is also very simple, but there are also many disadvantages, whether a file format can be played or not depends on whether you have installed the correct parser and decoder. Even if you have the universal interpreter installation package, many problems may occur, however, since the emergence of many open-source software, such as the well-known FFMPEG and vlc, everything has become simple and I will not discuss much about FFMPEG-based SDK development, this article describes how to develop your own Player Based on the vlc sdk. Everything is so simple.

1. Download VLC player first, Http://www.videolan.org/vlc, Install the SDK)2. you can develop your own test programs with VC 6.0/2003/2005/2008/2010 IDE. it is better to create a window program to create a console program. It doesn't matter because it only focuses on how to use the vlc sdk. set the include and Lib paths of the VLC in the project. Generally, the path of the vlc sdk is c: \ Program Files \ VideoLAN \ VLC \ sdk5. copy libvlc. DLL, Libvlccore. dll, plugins directory to the project DEBUG directory. If it is a release, copy it to the release directory. The directory structure is as follows:   

The complete code follows:

# Include <stdio. h> # include <tchar. h> # include <time. h> # include <windows. h> # include <VLC/VLC. h> # pragma comment (Lib, "libvlc. lib ") # pragma comment (Lib," libvlccore. lib ") int main (INT argc, char * argv []) {libvlc_instance_t * vlc_ins = NULL; libvlc_media_player_t * vlc_player = NULL; libvlc_media_t * vlc_media = NULL; const char * vlc_args [] = {"-I", "Dummy", "-- ignore-config", "-- extraintf = logger", "-- verbose = 2" ,}; // Create a VLC instance vlc_ins = libvlc_new (sizeof (vlc_args)/sizeof (vlc_args [0]), vlc_args); If (vlc_ins! = NULL) {// create a VLC player vlc_player = libvlc_media_player_new (vlc_ins); If (vlc_player! = NULL) {// create a media instance through the file path. Here is my test file vlc_media = libvlc_media_new_path (vlc_ins, "d: \ clips \ org.mp4 "); if (vlc_media! = NULL) {// parse the media instance libvlc_media_parse (vlc_media); // obtain the playback length of the media file and return mslibvlc_time_t duration = libvlc_media_get_duration (vlc_media ); // obtain information about multiple video and audio tracks and other types of tracks in the media. libvlc_media_track_info_t * media_tracks = NULL; int trackcount = libvlc_media_get_tracks_info (vlc_media, & media_tracks ); // here the memory is released, but there is a problem during the test. I have not carefully studied why // free (media_tracks); // crash? // Set the open media file to the player libvlc_media_player_set_media (vlc_player, vlc_media); // because it is a Windows system, you need to set an hwnd for the player as a window, the desktop window is used directly here. Here the test is only libvlc_media_player_set_hwnd (vlc_player,: get1_topwindow (); // start playing the video libvlc_media_player_play (vlc_player ); // This is only for printing some information. Exit time_t last_time = Time (null) after 20 seconds; while (Time (null) <(last_time + 20 ))) {sleep (10); // obtain the current playback position libvlc_time_t play_time = libvlc_media_player_get_time (vlc_player); printf ("playing time: % LLD Ms \ r", (_ int64) (play_time); // obtain the media status // libvlc_state_t media_state = libvlc_media_get_state (vlc_media); // printf ("\ nmedia state: % d \ n", (INT) (media_state);} // stop libvlc_media_player_stop (vlc_player); // release libvlc_media_release (vlc_media);} // release libvlc_media_player_release (vlc_player );} // release libvlc_release (vlc_ins);} return 0 ;}

 

Compile and run the video, and you can see the video on the desktop. It is so easy to use the vlc sdk as your own player.

 

The audio and video are actually the same. You can find the audio stream in the file and decompress it to obtain the audio frame data. You can also compress the data according to the specified encoding format, here I have made audio decoding and encoding into two projects, and I am also directly using the code:

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.