Convert Figma logo to code with AI

mpenkov logoffmpeg-tutorial

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

1,221
395
1,221
13

Top Related Projects

44,846

Mirror of https://git.ffmpeg.org/ffmpeg.git

FFmpeg libav tutorial - learn how media works from basic to transmuxing, transcoding and more. Translations: πŸ‡ΊπŸ‡Έ πŸ‡¨πŸ‡³ πŸ‡°πŸ‡· πŸ‡ͺπŸ‡Έ πŸ‡»πŸ‡³ πŸ‡§πŸ‡·

Python bindings for FFmpeg - with complex filtering support

Quick Overview

The mpenkov/ffmpeg-tutorial repository is an educational resource that provides a series of tutorials on using FFmpeg's libav* libraries in C programming. It aims to help developers understand how to work with audio and video files programmatically using these powerful libraries.

Pros

  • Comprehensive coverage of FFmpeg's libav* libraries
  • Step-by-step tutorials with clear explanations
  • Includes practical examples for common audio/video tasks
  • Regularly updated to keep pace with FFmpeg changes

Cons

  • Requires intermediate C programming knowledge
  • May be challenging for beginners in audio/video processing
  • Limited to C language, not covering other language bindings
  • Some examples may become outdated as FFmpeg evolves

Code Examples

  1. Opening a video file:
AVFormatContext *fmt_ctx = NULL;
if (avformat_open_input(&fmt_ctx, filename, NULL, NULL) < 0) {
    fprintf(stderr, "Could not open source file %s\n", filename);
    exit(1);
}
  1. Decoding a video frame:
AVPacket packet;
AVFrame *frame = av_frame_alloc();
int ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == 0) {
    printf("Frame decoded: %d\n", dec_ctx->frame_number);
}
av_frame_free(&frame);
  1. Encoding a video frame:
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;

int ret = avcodec_send_frame(enc_ctx, frame);
if (ret < 0) {
    fprintf(stderr, "Error sending a frame for encoding\n");
    exit(1);
}

while (ret >= 0) {
    ret = avcodec_receive_packet(enc_ctx, &pkt);
    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
        return;
    else if (ret < 0) {
        fprintf(stderr, "Error during encoding\n");
        exit(1);
    }
    fwrite(pkt.data, 1, pkt.size, output_file);
    av_packet_unref(&pkt);
}

Getting Started

  1. Clone the repository:

    git clone https://github.com/mpenkov/ffmpeg-tutorial.git
    
  2. Install FFmpeg development libraries:

    sudo apt-get install libavcodec-dev libavformat-dev libavutil-dev
    
  3. Navigate to a tutorial directory and compile:

    cd ffmpeg-tutorial/tutorial01
    gcc -o tutorial01 tutorial01.c -lavformat -lavcodec -lavutil
    
  4. Run the compiled program:

    ./tutorial01 path/to/your/video/file
    

Competitor Comparisons

44,846

Mirror of https://git.ffmpeg.org/ffmpeg.git

Pros of FFmpeg

  • Comprehensive multimedia framework with extensive features and capabilities
  • Actively maintained by a large community, ensuring regular updates and improvements
  • Supports a wide range of formats, codecs, and platforms

Cons of FFmpeg

  • Steep learning curve due to its complexity and vast functionality
  • Requires more setup and configuration for basic usage
  • Documentation can be overwhelming for beginners

Code Comparison

FFmpeg:

AVFormatContext *fmt_ctx = NULL;
AVCodecContext *video_dec_ctx = NULL;
AVStream *video_stream = NULL;
const char *src_filename = NULL;
int video_stream_idx = -1;

ffmpeg-tutorial:

AVFormatContext *pFormatContext = avformat_alloc_context();
if (!pFormatContext) {
    printf("ERROR could not allocate memory for Format Context");
    return -1;
}

Summary

FFmpeg is a powerful and comprehensive multimedia framework, while ffmpeg-tutorial is a simplified tutorial project aimed at helping beginners understand FFmpeg's basics. FFmpeg offers more features and ongoing development but can be more challenging for newcomers. The ffmpeg-tutorial provides a gentler introduction to FFmpeg concepts but lacks the full functionality of the main project.

FFmpeg libav tutorial - learn how media works from basic to transmuxing, transcoding and more. Translations: πŸ‡ΊπŸ‡Έ πŸ‡¨πŸ‡³ πŸ‡°πŸ‡· πŸ‡ͺπŸ‡Έ πŸ‡»πŸ‡³ πŸ‡§πŸ‡·

Pros of ffmpeg-libav-tutorial

  • More comprehensive coverage of FFmpeg concepts and features
  • Includes detailed explanations and diagrams for better understanding
  • Regularly updated with recent FFmpeg API changes

Cons of ffmpeg-libav-tutorial

  • May be overwhelming for absolute beginners
  • Focuses more on theory, with fewer practical examples
  • Requires more time investment to work through the material

Code Comparison

ffmpeg-tutorial:

AVFormatContext *pFormatCtx = NULL;
if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
    return -1; // Couldn't open file

ffmpeg-libav-tutorial:

AVFormatContext *input_format_context = NULL;
if (avformat_open_input(&input_format_context, input_file, NULL, NULL) != 0) {
    fprintf(stderr, "Could not open input file '%s'", input_file);
    return -1;
}

Both examples demonstrate opening an input file, but ffmpeg-libav-tutorial provides more detailed error handling and uses more descriptive variable names.

Python bindings for FFmpeg - with complex filtering support

Pros of ffmpeg-python

  • High-level Python interface for FFmpeg, simplifying complex operations
  • Extensive documentation and examples for various use cases
  • Active development and community support

Cons of ffmpeg-python

  • Requires FFmpeg to be installed separately
  • May have a steeper learning curve for users new to FFmpeg concepts

Code Comparison

ffmpeg-python:

import ffmpeg

stream = ffmpeg.input('input.mp4')
stream = ffmpeg.filter(stream, 'scale', 1280, 720)
stream = ffmpeg.output(stream, 'output.mp4')
ffmpeg.run(stream)

ffmpeg-tutorial:

AVFormatContext *pFormatCtx = NULL;
if(avformat_open_input(&pFormatCtx, filename, NULL, NULL)!=0)
    return -1; // Couldn't open file

Summary

ffmpeg-python provides a more Pythonic approach to working with FFmpeg, offering a high-level interface and extensive documentation. It's ideal for developers who prefer Python and want to simplify complex FFmpeg operations. However, it requires separate FFmpeg installation and may have a steeper initial learning curve.

ffmpeg-tutorial, on the other hand, offers a lower-level C implementation, providing deeper insights into FFmpeg's internal workings. It's better suited for those wanting to understand FFmpeg's core functionality or develop C applications using FFmpeg libraries directly.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

ffmpeg-tutorial


The original tutorials have now been updated. I won't be maintaining this project anymore, and am keeping it here for historical reasons.


This repository contains files from an FFmpeg tutorial originally written by Stephen Dranger (dranger@gmail.com). 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.

Main changes

  • 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

First, make sure you have a recent installation of FFmpeg. It's recommended that you build FFmpeg from source as described in this link.

To build the tutorials:

git clone git@github.com:chelyaev/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