首先官网下载源码文件:http://www.ffmpegmac.net/,注意点击“Download Now!"字样下面的“sourcecode”进行下载。下载后是一个.7z的压缩包,可以下载“The Unarchiver”程序进行解压,上传到资源栏里了,http://download.csdn.net/detail/enter_/9629936
这里有官网的编译方法:https://trac.ffmpeg.org/wiki/CompilationGuide/MacOSX。
首先最好是先把xcode装上,然后安装Homebrew ,它类似Ubuntu下面的 apt-get ,用习惯了apt-get 然后切换到mac下没有这样一个工具是一件很苦恼的事情,所以Homebrew就是apt-get一样好用。直接复制下面的代码到终端就好,这里不需要sudo,安装完使用的时候也不需要sudo。
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install automake fdk-aac git lame libass libtool libvorbis libvpx \ opus sdl shtool texi2html theora wget x264 xvid yasm
$sudo mkdir /usr/local/share/man/man1
$sudo chmod 777 /usr/local/share/man/man1
然后是配置configure,首先应该进入到解压后的ffmpeg目录下,然后在执行configure之前先在解压的ffmpeg目录下执行,直接复制到终端即可:
CFLAGS=`freetype-config --cflags` LDFLAGS=`freetype-config --libs` PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig
./configure --prefix=/usr/local --enable-gpl --enable-nonfree --enable-libass \ --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus \ --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --extra-ldflags=-L/usr/local/lib
注意上面命令最后的参数:--extra-ldflags=-L/usr/local/lib,这个是用于找到相应的库的,如上面的yasm等,这些命令在安装并完成link后一般都会出现在/usr/local/lib目录下,所以这里直接添加库目录进去。
如果碰到类似的错误: ERROR: xxx not found,yasm/nasm not found or too old. Use --disable-yasm for a crippled build. 或者:yasm-1.3.0 already installed, it's just not linked。编译ffmpeg会用到汇编用于加速。解决办法:
首先,在终端上:$brew install xxx,如这里可以$brew install yasm,安装相应的命令。
如果提示xxx已经安装只是just not linked。那么直接用brew连接就好:$brew link xxx,如这里可以:$brew link yasm。链接后会在/usr/local/lib下生成一个链接文件指向相应的库。
configure这步没有问题后就可以往下执行了,都是些老套路,和Linux下一样一样的。
$make
$sudo make install
这里顺便说下添加环境变量的做法,一般常规是这样:export PATH=$PATH:/xxx/xx/bin,但是这样做只会对当前的终端生效,想要永久生效的话可以直接在bash文件中添加配置,做法如下:
$vim ~/.bash_profile
然后在打开的bash_profile中添加:export PATH=$PATH:/xxx/xxx/bin
保存后退出重启电脑即可生效。以后添加配置或者环境变量的时候都可以直接添加在这个文件里面。
最后是编译好的ffmpeg的include、lib、bin。需要的可以直接下载,版本为3.1.3。
如果编译上还有问题的可以直接留言探讨。
然后是搭建ffmpeg的运行环境了,这个就比较简单了,在这里简单说说QT和自己写Makefile时的几个问题。
首先放个测试用例,这里会输出ffmpeg的配置信息:(文件名为main.cpp)
#include <stdio.h> extern "C" { #include "libavcodec/avcodec.h" } int main() { printf("%s\n", avcodec_configuration()); return 0; }
运行后输出结果如下:
--prefix=/usr/local --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --extra-ldflags=-L/usr/local/lib
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.11 LIBS += "/usr/local/lib/libavcodec.a" \ "/usr/local/lib/libavdevice.a" \ "/usr/local/lib/libavfilter.a" \ "/usr/local/lib/libavformat.a" \ "/usr/local/lib/libavutil.a" \ "/usr/local/lib/libpostproc.a" \ "/usr/local/lib/libswresample.a" \ "/usr/local/lib/libswscale.a" LIBS += -liconv SOURCES += main.cpp INCLUDEPATH += "/usr/local/include"
clang -I /usr/local/include -o main main.cpp /usr/local/lib/libavcodec.a /usr/local/lib/libavutil.a -liconv
error: symbol(s) not found for architecture x86_64
error: linker command failed with exit code 1 (use -v to see invocation)
那主要的原因还是库没有链接完整,第一个检查ffmpeg用到的库链接到了没有,如上面不能只写libavcodec.a,少了libavutil.a就有这样的问题。
然后就是是否忘了链接iconv库,因为ffmpeg库中用到了iconv库的。
还有就是如果你是在c++中使用ffmpeg,那么不能忘了上面加入头文件时的 extern “C"{},不然也会出现上面那个错误。
比如我第一次运行的时候就是上面有些库没有链接到,出现了下面这些信息,害我查了很久,错误信息如下:
Undefined symbols for architecture x86_64: "_av_bprint_clear", referenced from: _avcodec_decode_subtitle2 in libavcodec.a(utils.o) "_av_bprint_finalize", referenced from: _avcodec_decode_subtitle2 in libavcodec.a(utils.o) _avpriv_bprint_to_extradata in libavcodec.a(utils.o) "_av_bprint_init", referenced from: _avcodec_decode_subtitle2 in libavcodec.a(utils.o) "_av_bprintf", referenced from: _avcodec_decode_subtitle2 in libavcodec.a(utils.o) "_av_buffer_alloc", referenced from: _av_grow_packet in libavcodec.a(avpacket.o) _av_packet_merge_side_data in libavcodec.a(avpacket.o) _ff_thread_get_buffer in libavcodec.a(pthread_frame.o) "_av_buffer_allocz", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) "_av_buffer_create", referenced from: _av_packet_from_data in libavcodec.a(avpacket.o) "_av_buffer_default_free", referenced from: _av_packet_from_data in libavcodec.a(avpacket.o) "_av_buffer_pool_get", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) "_av_buffer_pool_init", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) "_av_buffer_pool_uninit", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) _avcodec_close in libavcodec.a(utils.o) "_av_buffer_realloc", referenced from: _avcodec_encode_audio2 in libavcodec.a(utils.o) _avcodec_encode_video2 in libavcodec.a(utils.o) _av_new_packet in libavcodec.a(avpacket.o) _av_grow_packet in libavcodec.a(avpacket.o) _copy_packet_data in libavcodec.a(avpacket.o) _av_packet_ref in libavcodec.a(avpacket.o) "_av_buffer_ref", referenced from: _ff_thread_ref_frame in libavcodec.a(utils.o) _copy_packet_data in libavcodec.a(avpacket.o) _av_packet_ref in libavcodec.a(avpacket.o) _avcodec_copy_context in libavcodec.a(options.o) "_av_buffer_unref", referenced from: _avcodec_close in libavcodec.a(utils.o) _av_packet_alloc in libavcodec.a(avpacket.o) _av_packet_unref in libavcodec.a(avpacket.o) _av_packet_free in libavcodec.a(avpacket.o) _av_copy_packet_side_data in libavcodec.a(avpacket.o) _copy_packet_data in libavcodec.a(avpacket.o) _av_free_packet in libavcodec.a(avpacket.o) ... "_av_chroma_location_name", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_color_primaries_name", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_color_range_name", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_color_space_name", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_color_transfer_name", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_cpu_count", referenced from: _ff_frame_thread_encoder_init in libavcodec.a(frame_thread_encoder.o) _ff_frame_thread_init in libavcodec.a(pthread_frame.o) _ff_slice_thread_init in libavcodec.a(pthread_slice.o) "_av_dict_copy", referenced from: _avcodec_open2 in libavcodec.a(utils.o) _ff_frame_thread_encoder_init in libavcodec.a(frame_thread_encoder.o) "_av_dict_free", referenced from: _avcodec_open2 in libavcodec.a(utils.o) _ff_frame_thread_encoder_init in libavcodec.a(frame_thread_encoder.o) "_av_dict_get", referenced from: _av_packet_pack_dictionary in libavcodec.a(avpacket.o) _ff_frame_thread_encoder_init in libavcodec.a(frame_thread_encoder.o) "_av_dict_set", referenced from: _av_packet_unpack_dictionary in libavcodec.a(avpacket.o) _ff_frame_thread_encoder_init in libavcodec.a(frame_thread_encoder.o) "_av_fast_realloc", referenced from: _ff_thread_release_buffer in libavcodec.a(pthread_frame.o) "_av_fifo_alloc_array", referenced from: _ff_frame_thread_encoder_init in libavcodec.a(frame_thread_encoder.o) "_av_fifo_freep", referenced from: _ff_frame_thread_encoder_free in libavcodec.a(frame_thread_encoder.o) "_av_fifo_generic_read", referenced from: _worker in libavcodec.a(frame_thread_encoder.o) "_av_fifo_generic_write", referenced from: _ff_thread_video_encode_frame in libavcodec.a(frame_thread_encoder.o) "_av_fifo_size", referenced from: _worker in libavcodec.a(frame_thread_encoder.o) "_av_frame_alloc", referenced from: _ff_reget_buffer in libavcodec.a(utils.o) _avcodec_open2 in libavcodec.a(utils.o) _avcodec_encode_audio2 in libavcodec.a(utils.o) _ff_thread_video_encode_frame in libavcodec.a(frame_thread_encoder.o) _ff_frame_thread_init in libavcodec.a(pthread_frame.o) _ff_snow_common_init in libavcodec.a(snow.o) "_av_frame_copy", referenced from: _ff_reget_buffer in libavcodec.a(utils.o) "_av_frame_copy_props", referenced from: _avcodec_encode_audio2 in libavcodec.a(utils.o) _unrefcount_frame in libavcodec.a(utils.o) "_av_frame_free", referenced from: _ff_reget_buffer in libavcodec.a(utils.o) _avcodec_open2 in libavcodec.a(utils.o) _avcodec_encode_audio2 in libavcodec.a(utils.o) _avcodec_close in libavcodec.a(utils.o) _worker in libavcodec.a(frame_thread_encoder.o) _ff_thread_video_encode_frame in libavcodec.a(frame_thread_encoder.o) _ff_frame_thread_free in libavcodec.a(pthread_frame.o) ... "_av_frame_get_buffer", referenced from: _avcodec_encode_audio2 in libavcodec.a(utils.o) "_av_frame_get_channels", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) _avcodec_encode_audio2 in libavcodec.a(utils.o) _unrefcount_frame in libavcodec.a(utils.o) _avcodec_decode_audio4 in libavcodec.a(utils.o) "_av_frame_get_color_range", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) "_av_frame_get_colorspace", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) "_av_frame_get_pkt_duration", referenced from: _avcodec_decode_audio4 in libavcodec.a(utils.o) "_av_frame_get_side_data", referenced from: _ff_side_data_update_matrix_encoding in libavcodec.a(utils.o) _avcodec_encode_audio2 in libavcodec.a(utils.o) _ff_alloc_a53_sei in libavcodec.a(utils.o) "_av_frame_is_writable", referenced from: _ff_reget_buffer in libavcodec.a(utils.o) "_av_frame_move_ref", referenced from: _ff_reget_buffer in libavcodec.a(utils.o) _unrefcount_frame in libavcodec.a(utils.o) _avcodec_receive_frame in libavcodec.a(utils.o) _ff_thread_decode_frame in libavcodec.a(pthread_frame.o) _ff_thread_release_buffer in libavcodec.a(pthread_frame.o) "_av_frame_new_side_data", referenced from: _ff_side_data_update_matrix_encoding in libavcodec.a(utils.o) _ff_init_buffer_info in libavcodec.a(utils.o) _avcodec_decode_audio4 in libavcodec.a(utils.o) "_av_frame_ref", referenced from: _ff_thread_ref_frame in libavcodec.a(utils.o) _ff_thread_video_encode_frame in libavcodec.a(frame_thread_encoder.o) "_av_frame_set_best_effort_timestamp", referenced from: _avcodec_decode_video2 in libavcodec.a(utils.o) _avcodec_decode_audio4 in libavcodec.a(utils.o) "_av_frame_set_channels", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) _avcodec_encode_audio2 in libavcodec.a(utils.o) _unrefcount_frame in libavcodec.a(utils.o) _avcodec_decode_audio4 in libavcodec.a(utils.o) "_av_frame_set_color_range", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) "_av_frame_set_colorspace", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) "_av_frame_set_pkt_duration", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) _avcodec_decode_audio4 in libavcodec.a(utils.o) "_av_frame_set_pkt_pos", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) _avcodec_decode_video2 in libavcodec.a(utils.o) "_av_frame_set_pkt_size", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) "_av_frame_unref", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) _ff_reget_buffer in libavcodec.a(utils.o) _avcodec_decode_video2 in libavcodec.a(utils.o) _unrefcount_frame in libavcodec.a(utils.o) _avcodec_decode_audio4 in libavcodec.a(utils.o) _avcodec_receive_frame in libavcodec.a(utils.o) _avcodec_flush_buffers in libavcodec.a(utils.o) ... "_av_free", referenced from: _avcodec_encode_audio2 in libavcodec.a(utils.o) _avpriv_bprint_to_extradata in libavcodec.a(utils.o) _default_lockmgr_cb in libavcodec.a(utils.o) _worker in libavcodec.a(frame_thread_encoder.o) _avcodec_alloc_context3 in libavcodec.a(options.o) _ff_slice_thread_init in libavcodec.a(pthread_slice.o) _ff_snow_alloc_blocks in libavcodec.a(snow.o) ... (maybe you meant: _av_free_packet) "_av_freep", referenced from: _av_fast_padded_malloc in libavcodec.a(utils.o) _av_fast_padded_mallocz in libavcodec.a(utils.o) _avcodec_fill_audio_frame in libavcodec.a(utils.o) _avcodec_default_get_buffer2 in libavcodec.a(utils.o) _ff_get_format in libavcodec.a(utils.o) _avcodec_open2 in libavcodec.a(utils.o) _avcodec_decode_subtitle2 in libavcodec.a(utils.o) ... "_av_gcd", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_get_bytes_per_sample", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_get_channel_layout_nb_channels", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) _avcodec_open2 in libavcodec.a(utils.o) "_av_get_channel_layout_string", referenced from: _avcodec_open2 in libavcodec.a(utils.o) _avcodec_string in libavcodec.a(utils.o) "_av_get_colorspace_name", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_get_cpu_flags", referenced from: _ff_me_cmp_init_x86 in libavcodec.a(me_cmp_init.o) _ff_dwt_init_x86 in libavcodec.a(snowdsp.o) _ff_h264qpel_init_x86 in libavcodec.a(h264_qpel.o) _ff_hpeldsp_init_x86 in libavcodec.a(hpeldsp_init.o) _ff_videodsp_init_x86 in libavcodec.a(videodsp_init.o) "_av_get_media_type_string", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_get_pix_fmt_name", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) _ff_reget_buffer in libavcodec.a(utils.o) _avcodec_open2 in libavcodec.a(utils.o) _avcodec_string in libavcodec.a(utils.o) "_av_get_planar_sample_fmt", referenced from: _avcodec_open2 in libavcodec.a(utils.o) "_av_get_sample_fmt_name", referenced from: _avcodec_open2 in libavcodec.a(utils.o) _avcodec_string in libavcodec.a(utils.o) "_av_image_check_sar", referenced from: _ff_set_sar in libavcodec.a(utils.o) _ff_init_buffer_info in libavcodec.a(utils.o) _avcodec_open2 in libavcodec.a(utils.o) "_av_image_check_size", referenced from: _avcodec_set_dimensions in libavcodec.a(utils.o) _ff_set_dimensions in libavcodec.a(utils.o) _ff_get_buffer in libavcodec.a(utils.o) _avcodec_open2 in libavcodec.a(utils.o) _avcodec_encode_video2 in libavcodec.a(utils.o) _avcodec_decode_video2 in libavcodec.a(utils.o) _apply_param_change in libavcodec.a(utils.o) ... "_av_image_fill_linesizes", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) "_av_image_fill_pointers", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) "_av_log", referenced from: _av_fast_padded_malloc in libavcodec.a(utils.o) _av_fast_padded_mallocz in libavcodec.a(utils.o) _avcodec_set_dimensions in libavcodec.a(utils.o) _ff_set_sar in libavcodec.a(utils.o) _ff_color_frame in libavcodec.a(utils.o) _avcodec_default_get_buffer2 in libavcodec.a(utils.o) _ff_init_buffer_info in libavcodec.a(utils.o) ... (maybe you meant: _av_log_ask_for_sample, _av_log_missing_feature ) "_av_log_get_level", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_malloc", referenced from: _ff_get_format in libavcodec.a(utils.o) _unrefcount_frame in libavcodec.a(utils.o) _default_lockmgr_cb in libavcodec.a(utils.o) _av_copy_packet_side_data in libavcodec.a(avpacket.o) _av_packet_split_side_data in libavcodec.a(avpacket.o) _ff_frame_thread_init in libavcodec.a(pthread_frame.o) _avcodec_alloc_context3 in libavcodec.a(options.o) ... "_av_mallocz", referenced from: _av_fast_padded_malloc in libavcodec.a(utils.o) _av_fast_padded_mallocz in libavcodec.a(utils.o) _avcodec_fill_audio_frame in libavcodec.a(utils.o) _avcodec_default_get_buffer2 in libavcodec.a(utils.o) _ff_get_format in libavcodec.a(utils.o) _avcodec_open2 in libavcodec.a(utils.o) _av_cpb_properties_alloc in libavcodec.a(utils.o) ... "_av_match_list", referenced from: _avcodec_open2 in libavcodec.a(utils.o) "_av_mul_q", referenced from: _avcodec_open2 in libavcodec.a(utils.o) _avcodec_decode_video2 in libavcodec.a(utils.o) "_av_opt_copy", referenced from: _avcodec_copy_context in libavcodec.a(options.o) "_av_opt_free", referenced from: _avcodec_open2 in libavcodec.a(utils.o) _avcodec_close in libavcodec.a(utils.o) _ff_frame_thread_free in libavcodec.a(pthread_frame.o) _avcodec_copy_context in libavcodec.a(options.o) "_av_opt_set", referenced from: _init_context_defaults in libavcodec.a(options.o) "_av_opt_set_defaults", referenced from: _avcodec_open2 in libavcodec.a(utils.o) _init_context_defaults in libavcodec.a(options.o) "_av_opt_set_defaults2", referenced from: _init_context_defaults in libavcodec.a(options.o) "_av_opt_set_dict", referenced from: _avcodec_open2 in libavcodec.a(utils.o) "_av_pix_fmt_count_planes", referenced from: _ff_get_buffer in libavcodec.a(utils.o) "_av_pix_fmt_desc_get", referenced from: _avcodec_align_dimensions2 in libavcodec.a(utils.o) _avcodec_align_dimensions in libavcodec.a(utils.o) _ff_color_frame in libavcodec.a(utils.o) _avcodec_default_get_buffer2 in libavcodec.a(utils.o) _ff_get_buffer in libavcodec.a(utils.o) _avcodec_default_get_format in libavcodec.a(utils.o) _ff_get_format in libavcodec.a(utils.o) ... "_av_realloc", referenced from: _av_packet_add_side_data in libavcodec.a(avpacket.o) _av_packet_new_side_data in libavcodec.a(avpacket.o) _av_packet_pack_dictionary in libavcodec.a(avpacket.o) _av_packet_copy_props in libavcodec.a(avpacket.o) _ff_side_data_set_encoder_stats in libavcodec.a(avpacket.o) "_av_realloc_array", referenced from: _ff_add_cpb_side_data in libavcodec.a(utils.o) "_av_reallocp_array", referenced from: _ff_thread_decode_frame in libavcodec.a(pthread_frame.o) "_av_reduce", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_rescale_q", referenced from: _avcodec_encode_audio2 in libavcodec.a(utils.o) _avcodec_decode_audio4 in libavcodec.a(utils.o) _avcodec_decode_subtitle2 in libavcodec.a(utils.o) _av_packet_rescale_ts in libavcodec.a(avpacket.o) "_av_sample_fmt_is_planar", referenced from: _avcodec_fill_audio_frame in libavcodec.a(utils.o) _avcodec_default_get_buffer2 in libavcodec.a(utils.o) _avcodec_encode_audio2 in libavcodec.a(utils.o) "_av_samples_copy", referenced from: _avcodec_encode_audio2 in libavcodec.a(utils.o) _avcodec_decode_audio4 in libavcodec.a(utils.o) "_av_samples_fill_arrays", referenced from: _avcodec_fill_audio_frame in libavcodec.a(utils.o) "_av_samples_get_buffer_size", referenced from: _avcodec_fill_audio_frame in libavcodec.a(utils.o) _avcodec_default_get_buffer2 in libavcodec.a(utils.o) "_av_samples_set_silence", referenced from: _avcodec_encode_audio2 in libavcodec.a(utils.o) "_av_strdup", referenced from: _avcodec_decode_subtitle2 in libavcodec.a(utils.o) "_av_strlcat", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_strlcatf", referenced from: _avcodec_string in libavcodec.a(utils.o) "_av_vlog", referenced from: _av_log_ask_for_sample in libavcodec.a(utils.o) "_avpriv_frame_get_metadatap", referenced from: _ff_init_buffer_info in libavcodec.a(utils.o) "_avpriv_set_systematic_pal2", referenced from: _avcodec_default_get_buffer2 in libavcodec.a(utils.o) "_iconv", referenced from: _avcodec_decode_subtitle2 in libavcodec.a(utils.o) "_iconv_close", referenced from: _avcodec_open2 in libavcodec.a(utils.o) _avcodec_decode_subtitle2 in libavcodec.a(utils.o) "_iconv_open", referenced from: _avcodec_open2 in libavcodec.a(utils.o) _avcodec_decode_subtitle2 in libavcodec.a(utils.o) ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)