Skip to content

A set of tutorials that demonstrates how to write a video player based on FFmpeg

Notifications You must be signed in to change notification settings

illuusio/ffmpeg-tutorial

 
 

Repository files navigation

ffmpeg-tutorial

This repository is fork from original repo

Mainly because there is audio broken and code is not keep in shape in order to maintain maximum compatibility to tutorial. All the warnings are also removed so there is no depricated code.

In Makefile there is possibility to use libavresample or libswresample which you have installed is not auto detected! They are under run time which

Use libavresampler in gcc: -D__RESAMPLER__ -D__LIBAVRESAMPLE__ Use libswresampler in gcc: -D__RESAMPLER__ -D__LIBSWRESAMPLE__

all the magic happens in function

int audio_tutorial_resample(VideoState *is, struct AVFrame *inframe)

that you feed original frame in and it'll convert it it to what you want

  • FLTP is 32-bit Float Planar (Ogg/Vorbis, OPUS, MP4, WMA and many more)
  • S16P is Stereo 16-bit Planar (MP3, MPEG2 and MPEG1)
  • S16 is SDL output format that is not planar

Planar means if I get it right

Right--------------------
RRRRRRRRRRRRRRRRRRRRRRRRR
Left---------------------
LLLLLLLLLLLLLLLLLLLLLLLLL
-------------------------

So you samples are in order that first there is right channel and after that there is left channel in package. In case of normal PCM S16 you have

Right/Left--------------
RRLLRRLLRRLLRRLLRRLLRRLL
------------------------

That's why audio sound so strange without resample and if it is float are thing totally differently from this.

Mainly this repository contains files from an FFmpeg tutorial originally written by Stephen Dranger ([email protected]). The files have been updated to work with the most recent version of FFmpeg (see VERSION.txt for the most recent version at the time of writing). The updates were performed with an effort to modify as little code as possible, so that the original code and tutorial descriptions could be easily consulted.

The code from the original tutorial and the accompanying description is located here.

Coding coventions

This repository is styled with

astyle --indent=spaces=4 -K -S -j -c -F -f --style=java -p --lineend=linux

So it won't merge with original anymore and if you want to make pull request all the requests should be also styled as that astyle line (with hand or astyle).

Example looks like this

    codec = avcodec_find_decoder(codecCtx->codec_id);

    if(!codec || (avcodec_open2(codecCtx, codec, &optionsDict) < 0)) {
        fprintf(stderr, "Unsupported codec!\n");
        return -1;
    }

    switch(codecCtx->codec_type) {
        case AVMEDIA_TYPE_AUDIO:
            is->audioStream = stream_index;
            is->audio_st = pFormatCtx->streams[stream_index];
            is->audio_buf_size = 0;
            is->audio_buf_index = 0;
            memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
            packet_queue_init(&is->audioq);
            SDL_PauseAudio(0);
            break;

Main changes

  • Added stuff to resample audio to correct output so it really works
  • Renamed includes, e.g. ffmpeg/avcodec.h --> libavcodec/avcodec.h
  • Work around deprecated functions and symbols (see below)
  • Initializing pointers to NULL on declaration. Some FFmpeg functions (e.g. avformat_open_input) now segfault when given uninitialized pointers as input.
  • Removed tutorial08.c, which introduced software scaling (as opposed to using the img_convert method). img_convert has been deprecated and is no longer available, so these new tutorials use software scaling from the very beginning, and a separate tutorial is not necessary.

Deprecated Functions and Symbols

This section describes the changes made to work around deprecated functions and symbols, in the format: before --> after. In some cases, a simple rename sufficed (e.g. dump_format), but in others, more significant changes to the code were required (e.g. avcodec_decode_audio2). Consult the diffs for each respective tutorial to see exactly what has changed since the original version of the tutorial.

  • av_open_input_file --> avformat_open_input
  • av_find_stream_info --> avformat_find_stream_info
  • dump_format --> av_dump_format
  • CODEC_TYPE_VIDEO --> AVMEDIA_TYPE_VIDEO
  • avcodec_open --> avcodec_open2
  • avcodec_decode_video --> avcodec_decode_video2
  • img_convert --> sws_scale
  • av_close_input_file --> avformat_close_input
  • avcodec_decode_audio2 --> avcodec_decode_audio4
  • CODEC_TYPE_AUDIO --> AVMEDIA_TYPE_AUDIO
  • url_set_interrupt_cb --> avio_open2
  • url_ferror --> check attribute is->pFormatCtx->pb->error
  • pstrcpy --> av_strlcpy

Building and Running

You should have at least FFMPEG 1.0 installed. This is tested with and should work. If you have FFMPEG 2.0 or higher installed you can use Resampler code. Those libraries are also with pre FFMPEG 1.0 but they won't necessary work as expected.

Ubuntu/Debian (Mint also) have preset of using libav (avconv) and it should work but the will be some minor and major difference's in quality so it's recommended that you build FFmpeg from source as described in this link.

To build the tutorials:

git clone https://github.com/illuusio/ffmpeg-tutorial.git
cd ffmpeg-tutorial
make

To run a tutorial, first make sure that your ffmpeg installation is on your $LD_LIBRARY_PATH and then:

bin/tutorial01.out audio/videofile

About

A set of tutorials that demonstrates how to write a video player based on FFmpeg

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 100.0%