Results tagged “ffmpeg” from muse

FFmpeg for RTSP to RTMP conversion

|
ffmpeg -rtsp_transport tcp -i rtsp://user:password@localhost:554/rtsp -vcodec copy -f flv -r 25 -s 1920x1080 -an "rtmp://localhost/rtmp"

-an: Disable audio

-f: Force format

-r: Frame rate

-s: Video resolution

-vcodec: Set the video codec. "copy" means no decoding/encoding.

FFmpeg is so awesome.

Custom IO with FFmpeg

|

There are a few articles on the internet about this topic, but they are kind of outdated so I'm writing a new one.

Here is the source code for the impatient.

The idea is simple, just define your own callback methods and buffers, then tell ffmpeg to use them. In my code, I've wrapped up the variables into a class to pass things around easily.

There are a few things to pay attention to. Use av_malloc() and av_free() to allocate and free your buffers, ffmpeg aligns the memory it allocates which should speed things up. Free the buffer first then free AVIOContext, ffmpeg does something funny with the buffer, don't rely on ffmpeg to free it otherwise you will run into troubles (I got double free errors). It's also necessary to discover the input format yourself, either probe it or set it. And even if you do your own IO, don't count on calling avformat_open_input() from multiple threads at the same time.

Below is how to open a file using the class I wrote. Note that an empty filename is passed into avformat_open_input().

MyIOContext *ioCtx = new MyIOContext("random_file");
AVFormatContext *avFormatCtx = avformat_alloc_context();
ioCtx->initAVFormatContext(avFormatCtx);
if (avformat_open_input(&avFormatCtx, "", NULL, NULL) != 0) {
    // handle error
}
// ...
avformat_close_input(&avFormatCtx);

References:

I was following this tutorial but had some trouble compiling, especially when I wanted to link with FFmpeg libraries, so I decided to share the makefile and save others some time in the future. My platform is Mac OS X 10.8 (Mountain Lion).

It's worth pointing out that I compiled FFmpeg and SDL from source, and installed a bunch of dependent libraries using macports.

CXX = g++
CXXFLAGS = -Wall -Wextra -pedantic -Os -I. -fPIC \
           -Wno-long-long -DNDEBUG
LDFLAGS = -framework OpenGL -framework GLUT
SH=bash

# don't forget to update the directory
FFMPEG_LIB_DIR = /mylibs/ffmpeg
FFMPEG_LIBS = -L$(FFMPEG_LIB_DIR)/libavcodec -lavcodec
FFMPEG_LIBS += -L$(FFMPEG_LIB_DIR)/libavformat -lavformat
FFMPEG_LIBS += -L$(FFMPEG_LIB_DIR)/libavutil -lavutil
FFMPEG_LIBS += -L$(FFMPEG_LIB_DIR)/libswresample -lswresample
FFMPEG_LIBS += -L$(FFMPEG_LIB_DIR)/libswscale -lswscale
FFMPEG_INCLUDES = -I$(FFMPEG_LIB_DIR)/libavcodec
FFMPEG_INCLUDES += -I$(FFMPEG_LIB_DIR)/libavformat
FFMPEG_INCLUDES += -I$(FFMPEG_LIB_DIR)/libavutil
FFMPEG_INCLUDES += -I$(FFMPEG_LIB_DIR)/libswresample
FFMPEG_INCLUDES += -I$(FFMPEG_LIB_DIR)/libswscale

# ffmpeg needs zlib
ZLIB_LIBS = $(shell pkg-config --libs zlib)

SDL_LIBS = $(shell sdl2-config --libs)
SDL_INCLUDES = $(shell sdl2-config --cflags)

CXXFLAGS += $(SDL_INCLUDES)
CXXFLAGS += $(FFMPEG_INCLUDES)
LDFLAGS += $(SDL_LIBS)
LDFLAGS += $(ZLIB_LIBS)
LDFLAGS += $(FFMPEG_LIBS)

SRCS = $(wildcard *.cpp)
OBJS = $(SRCS:.cpp=.o)
EXE = program

all: $(SRCS) $(EXE)

%.o: %.cpp
    $(CXX) -c -o $*.o $(CXXFLAGS) $*.cpp

$(EXE): $(OBJS)
    $(CXX) -o $@ $(OBJS) $(LDFLAGS)

clean:
    rm $(OBJS) $(EXE)

And here are the headers to include:

#include <OpenGL/OpenGL.h>
#include <OpenGL/gl3.h>