Skip to content

Instantly share code, notes, and snippets.

@irieger
Last active Feb 23, 2018
Embed
What would you like to do?
ffmpeg compile and link test
cmake_minimum_required( VERSION 2.8 FATAL_ERROR )
project( ffmpeg-linking-test )
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/" )
set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -O0 -g" )
set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -O2" )
if( NOT CMAKE_BUILD_TYPE )
set( CMAKE_BUILD_TYPE Debug )
endif()
if( CMAKE_VERSION VERSION_LESS "3.1" )
if( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set( CMAKE_CXX_FLAGS "--std=gnu++11 ${CMAKE_CXX_FLAGS}" )
endif()
else()
set( CMAKE_CXX_STANDARD 11 )
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_definitions("-x c++")
endif()
find_package( OpenMP )
if( OPENMP_FOUND )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}" )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}" )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}" )
endif()
# :Dependencies:
find_package( OpenImageIO REQUIRED )
include_directories( ${OpenImageIO_INCLUDE_DIR} )
set( LIBS ${LIBS} ${OpenImageIO_LIBRARY} )
find_package( FFMPEG REQUIRED )
include_directories( ${FFMPEG_INCLUDE_DIR} )
set( LIBS ${LIBS} ${FFMPEG_LIBRARIES} )
set( TEST_EXECUTABLE ffmpeg-encode-test )
add_executable( ${TEST_EXECUTABLE} main.cpp )
target_link_libraries( ${TEST_EXECUTABLE} ${LIBS} )
Executed in /home/pi/dev/test/ffmpeg-encode/build via `make VERBOSE=1` after `cmake ../`
/usr/bin/cmake -H/home/pi/dev/test/ffmpeg-encode -B/home/pi/dev/test/ffmpeg-encode/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/pi/dev/test/ffmpeg-encode/build/CMakeFiles /home/pi/dev/test/ffmpeg-encode/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/pi/dev/test/ffmpeg-encode/build'
make -f CMakeFiles/ffmpeg-encode-test.dir/build.make CMakeFiles/ffmpeg-encode-test.dir/depend
make[2]: Entering directory '/home/pi/dev/test/ffmpeg-encode/build'
cd /home/pi/dev/test/ffmpeg-encode/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/pi/dev/test/ffmpeg-encode /home/pi/dev/test/ffmpeg-encode /home/pi/dev/test/ffmpeg-encode/build /home/pi/dev/test/ffmpeg-encode/build /home/pi/dev/test/ffmpeg-encode/build/CMakeFiles/ffmpeg-encode-test.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/pi/dev/test/ffmpeg-encode/build'
make -f CMakeFiles/ffmpeg-encode-test.dir/build.make CMakeFiles/ffmpeg-encode-test.dir/build
make[2]: Entering directory '/home/pi/dev/test/ffmpeg-encode/build'
[ 50%] Building CXX object CMakeFiles/ffmpeg-encode-test.dir/main.cpp.o
/usr/bin/c++ -I/home/pi/bin/ffmpeg/include/libavformat -I/home/pi/bin/ffmpeg/include/libavutil -I/home/pi/bin/ffmpeg/include/libavcodec -I/home/pi/bin/ffmpeg/include/libswscale -I/home/pi/bin/ffmpeg/include/libavdevice -I/home/pi/bin/ffmpeg/include/libavdevice/.. -fopenmp -g -Wall -O0 -g -x c++ -std=gnu++11 -o CMakeFiles/ffmpeg-encode-test.dir/main.cpp.o -c /home/pi/dev/test/ffmpeg-encode/main.cpp
/home/pi/dev/test/ffmpeg-encode/main.cpp:20:16: warning: invalid suffix on literal; C++11 requires a space between literal and string macro [-Wliteral-suffix]
printf("Send frame %3"PRId64"\n", frame->pts);
^
/home/pi/dev/test/ffmpeg-encode/main.cpp:37:16: warning: invalid suffix on literal; C++11 requires a space between literal and string macro [-Wliteral-suffix]
printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
^
/home/pi/dev/test/ffmpeg-encode/main.cpp:13:13: warning: ‘void encode(AVCodecContext*, AVFrame*, AVPacket*, FILE*)’ defined but not used [-Wunused-function]
static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
^~~~~~
[100%] Linking CXX executable ffmpeg-encode-test
/usr/bin/cmake -E cmake_link_script CMakeFiles/ffmpeg-encode-test.dir/link.txt --verbose=1
/usr/bin/c++ -fopenmp -g -Wall -O0 -g CMakeFiles/ffmpeg-encode-test.dir/main.cpp.o -o ffmpeg-encode-test -Wl,-rpath,/home/pi/bin/ffmpeg/lib -rdynamic -lOpenImageIO /home/pi/bin/ffmpeg/lib/libavformat.so /home/pi/bin/ffmpeg/lib/libavcodec.so /home/pi/bin/ffmpeg/lib/libavutil.so /home/pi/bin/ffmpeg/lib/libswscale.so /home/pi/bin/ffmpeg/lib/libavdevice.so -lz
/usr/bin/ld: warning: libavcodec.so.57, needed by //usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4, may conflict with libavcodec.so.58
/usr/bin/ld: warning: libavformat.so.57, needed by //usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4, may conflict with libavformat.so.58
/usr/bin/ld: warning: libavutil.so.55, needed by //usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4, may conflict with libavutil.so.56
/usr/bin/ld: warning: libswscale.so.4, needed by //usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4, may conflict with libswscale.so.5
CMakeFiles/ffmpeg-encode-test.dir/main.cpp.o: In function `encode(AVCodecContext*, AVFrame*, AVPacket*, _IO_FILE*)':
/home/pi/dev/test/ffmpeg-encode/main.cpp:22: undefined reference to `avcodec_send_frame(AVCodecContext*, AVFrame const*)'
/home/pi/dev/test/ffmpeg-encode/main.cpp:29: undefined reference to `avcodec_receive_packet(AVCodecContext*, AVPacket*)'
/home/pi/dev/test/ffmpeg-encode/main.cpp:39: undefined reference to `av_packet_unref(AVPacket*)'
collect2: error: ld returned 1 exit status
CMakeFiles/ffmpeg-encode-test.dir/build.make:101: recipe for target 'ffmpeg-encode-test' failed
make[2]: *** [ffmpeg-encode-test] Error 1
make[2]: Leaving directory '/home/pi/dev/test/ffmpeg-encode/build'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/ffmpeg-encode-test.dir/all' failed
make[1]: *** [CMakeFiles/ffmpeg-encode-test.dir/all] Error 2
make[1]: Leaving directory '/home/pi/dev/test/ffmpeg-encode/build'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
#include <iostream>
#include <string>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <OpenImageIO/imageio.h>
using namespace OIIO;
static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
FILE *outfile)
{
int ret;
/* send the frame to the encoder */
if (frame)
printf("Send frame %3"PRId64"\n", frame->pts);
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);
}
printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
fwrite(pkt->data, 1, pkt->size, outfile);
av_packet_unref(pkt);
}
}
int main(int argc, char* argv[])
{
std::cout << "ffmpeg encode test code" << std::endl;
std::string base_path = "/home/pi/jpg-test-sequence/imgseq_000";
uint32_t start_img = 86400;
uint32_t number_of_frames = 4602;
ImageInput *in = ImageInput::open(base_path + std::to_string(start_img) + ".jpg");
if (!in)
return 1;
const ImageSpec &spec = in->spec();
int xres = spec.width;
int yres = spec.height;
int channels = spec.nchannels;
in->close();
ImageInput::destroy(in);
std::vector<uint8_t> pixels(xres*yres*channels);
std::cout << "Image sequence with " << xres << "x" << yres << " with " << channels << " channels" << std::endl;
for (uint32_t frame_num = start_img; frame_num < start_img + number_of_frames; frame_num++)
{
std::string img_name = base_path + std::to_string(frame_num) + ".jpg";
std::cout << "Image Name: " << img_name << std::endl;
ImageInput *in = ImageInput::open(img_name);
if (!in)
return 1;
in->read_image(TypeDesc::UINT8, &pixels[0]);
in->close();
ImageInput::destroy(in);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment