【音视频】FFmpeg 安装 Api使用 Android 笔记(一)

FFmpeg安装 文件处理Api使用1:Windows下编译安装FFmpeghttps://www.imooc.com/article/247113(音频专家李超老师的手记,老师慕课网视频:《FFmpeg音视频核心技术精讲与实战》)2:FFmpeg在Linux(ubuntu)下编译使用2.1安装yasmYasm是什么Ubuntu包安装sudoapt-getinstallyasm源码安装:wgeth...

� � � � � � � � � � � � �FFmpeg 安装 文件处理Api使用

1:Windows下编译安装 FFmpeg

� � � � �https://www.imooc.com/article/247113

� � � (音频专家李超老师的手记,老师慕课网视频:《FFmpeg音视频核心技术精讲与实战》

2:FFmpeg在Linux(ubuntu)下编译使用

� 2.1 安装yasm�Yasm是什么

� � � � ��Ubuntu包安装
� � � � � � � � ��� sudo apt-get install yasm

� � � � � 源码安装:

� � � � � � � � � �wget�http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
� � � � � � � � � �tar zxvf yasm-1.3.0.tar.gz
� � � � � � � � � �cd yasm-1.3.0
� � � � � � � � � ./configure
� � � � � � � � � �make && �make install� ��

��2.2 编译安装fdk-aac��AAC是什么

� � �wget�https://jaist.dl.sourceforge.net/project/opencore-amr/fdk-aac/fdk-aac-0.1.6.tar.gz� � � � � �tar xvf fdk-aac-0.1.6.tar.gz� � � � � �cd fdk-aac-0.1.6� � � � � �./configure� � � � � �make && sudo make install

2.3 安装 lame�lame是什么?

� � �Ubuntu包安装
� � � � � � � � ��� sudo apt-get install yasm

� � � �源码安装:

� � � � � � � � � wget�http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
� � � � � � � � � tar -xzf lame-3.99.5.tar.gz
� � � � � � � � � cd lame-3.99.5
� � � � � � � � �./configure
� � � � � � � � � make && sudo make install

2.4�安装 nasm��nasm是什么

� � � Ubuntu包安装
� � � � � � � � ��� sudo apt-get install nasm

� � � �源码安装:

� � � � � � � � � �wget�https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/nasm-2.13.03.tar.gz
� � � � � � � � � �tar xvf nasm-2.13.03.tar.gz
� � � � � � � � � �cd nasm-2.13.03
� � � � � � � � � �./configure
� � � � � � � � � �make && sudo make install�

2.4�安装 x264��什么是X264

� � � � � � � � � �wget�http://mirror.yandex.ru/mirrors/ftp.videolan.org/x264/snapshots/last_x264.tar.bz2
� � � � � � � � � �bunzip2 last_x264.tar.bz2
� � � � � � � � � �tar -vxf last_x264.tar
� � � � � � � � � �cd last_x264
� � � � � � � � � �./configure --enable-static --enable-shared --disable-asm --disable-avs
� � � � � � � � � �make && sudo make install

安装ffmpeg FFmpeg是什么

� � � � � � Ubuntu包安装

� � � � � � � � � � ��apt �install ffmpeg� 【可能版本较低,安装之前可以先apt-get� updtae】

� � � � � � 源码安装:

� � � � � � � � � � � ��FFmpeg官网网站下载地址

� � � � � � � � � � � �1:git地址� � � � � � � � � �git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg

� � � � � � � � � � � �2:压缩包地址� � � � � � �https://ffmpeg.org/releases/ffmpeg-4.1.3.tar.bz2

� � � � � � � � � � � �cd ->� 拉取&下载解压后的代码 目录下

� � � � � � � � � � � �执行:

� � � � � � � � � � � � �� ./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-small �--arch=x86_64 --enable-nonfree --� � � � � � � � � � � � � � � � � � � enable-libfdk-aac --enable-libx264 �--enable-filter=delogo --enable-debug --disable-optimizations --� � � � � � � � � � � � � � � � � � � enable-shared

� � � � � � � � � � � � � ��make && sudo make install

安装ffmpeg后续操作

上面我们编译完了FFMPEG之后可以去运行以下bin目录下生成的可执行文件

� � � � root@poc-virtual-machine:/usr/local/ffmpeg/bin# ./ffmpeg �-help
./ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open shared object file: No such file or directory

� � � � � 发现系统提示找不到动态库,可以用

� � �� root@poc-virtual-machine:/usr/local/ffmpeg/bin# � �ldd ffmpeg

� ��

root@poc-virtual-machine:/usr/local/ffmpeg/bin# ldd ffmpeg�� �linux-vdso.so.1 (0x00007ffebe789000)�� �libavdevice.so.58 => not found�� �libavfilter.so.7 => not found�� �libavformat.so.58 => not found�� �libavcodec.so.58 => not found�� �libpostproc.so.55 => not found�� �libswresample.so.3 => not found�� �libswscale.so.5 => not found�� �libavutil.so.56 => not found�� �libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe61c757000)�� �libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe61c538000)�� �libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe61c147000)�� �/lib64/ld-linux-x86-64.so.2 (0x00007fe61cd2a000)

� �不想编译FFMPEG之后还要动自己系统的环境,这时有一个简单的方法可以解决这个问题。就是在当前终端export一个环 境变量:

� /usr/local/ffmpeg/bin#�� �export LD_LIBRARY_PATH=../lib/

再次执行��ldd ffmpeg

� ��

root@poc-virtual-machine:/usr/local/ffmpeg/bin# ldd ffmpeg��� �linux-vdso.so.1 (0x00007ffcba331000)�� �libavdevice.so.58 => ../lib/libavdevice.so.58 (0x00007fd9ad168000)�� �libavfilter.so.7 => ../lib/libavfilter.so.7 (0x00007fd9acd0c000)�� �libavformat.so.58 => ../lib/libavformat.so.58 (0x00007fd9ac937000)�� �libavcodec.so.58 => ../lib/libavcodec.so.58 (0x00007fd9ab65d000)�� �libpostproc.so.55 => ../lib/libpostproc.so.55 (0x00007fd9ab44a000)�� �libswresample.so.3 => ../lib/libswresample.so.3 (0x00007fd9ab230000)�� �libswscale.so.5 => ../lib/libswscale.so.5 (0x00007fd9aafba000)�� �libavutil.so.56 => ../lib/libavutil.so.56 (0x00007fd9aad6b000)�� �libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd9aa9cd000)�� �libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd9aa7ae000)�� �libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd9aa3bd000)�� �libfdk-aac.so.1 => /usr/local/lib/libfdk-aac.so.1 (0x00007fd9aa0ff000)�� �libx264.so.157 => /usr/local/lib/libx264.so.157 (0x00007fd9a9c48000)�� �/lib64/ld-linux-x86-64.so.2 (0x00007fd9ad5aa000)�� �libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd9a9a44000)

�如果仍有

  XX.so.55 => not found

� � � 则 find� / -name xx.so� 找到�so库后 cp 拷入� xx//ffmpeg/lib� 下即可正常 【若找不到,则请安装对应的库】

root@poc-virtual-machine:/usr/local/ffmpeg/bin# ./ffmpeg �-help� � � �【查看命令使用】

root@poc-virtual-machine:/usr/local/ffmpeg/bin# ./ffmpeg �-version� 【查看当前FFmpeg版本】 并列出�configuration

ffmpeg version 4.1.3 Copyright (c) 2000-2019 the FFmpeg developers
� built with gcc 7 (Ubuntu 7.4.0-1ubuntu1~18.04)
� configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-small --arch=x86_64 --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-filter=delogo --enable-debug --disable-optimizations --enable-shared
� libavutil � � �56. 22.100 / 56. 22.100��

...省略.....��

�FFmpeg 的日志使用

� �通过av_log_set_level()设置当前Log的级别�

� �通过�v_log()来输入日志

� �例子:

#include <stdio.h>#include <libavutil/log.h>int main(){ av_log_set_level(AV_LOG_DEBUG); printf("Hello, World! \n"); av_log(NULL, AV_LOG_DEBUG, "Hello World FFmpeg!!\n"); return 0;}  

��gcc -g -o test test.c -I/home/FFmpeg/ffmpeg/ffmpeg-4.1.3 � -lavutil� � � � � 其中-l 后跟着FFmpeg的源码路径 -l后跟着Lib的 名称(Libxxx� ,xxx为库的名称,编译的时候填入xxx即可� )

ffmpeg对文件的重命名和删除操作�

avpriv_io_delete("file_path");� � 其中file_path 可以使��url 格式

avpriv_io_move("file_path1","file_path2");�� 其中file_path1 重命名为->file_path2

代码:

#include <stdio.h>#include <libavutil/log.h>#include <libavformat/avformat.h>int main(){ av_log_set_level(AV_LOG_DEBUG); int result = avpriv_io_delete("./a.txt"); if(result < 0){  av_log(NULL,AV_LOG_ERROR,"delete file is failed. \n");  return -1; }else {  av_log(NULL,AV_LOG_ERROR,"delete file  success. \n"); } //move & renamefile  result = avpriv_io_move("./1.txt","./2.txt"); if(result < 0){  av_log(NULL,AV_LOG_ERROR,"rename file is failed. \n");  return -1; }else {  av_log(NULL,AV_LOG_ERROR,"rename file  success. \n"); } return 0;}~

编译:gcc -g -o test test.c -I/home/FFmpeg/ffmpeg/ffmpeg-4.1.3 � -lavutil -lavformat
创建测试文件:�touch a.txt && touch �1.txt

FFmpeg操作目录(ls)

AVIODirContext *ctx =NULL;� �AVIOContext是FFmpeg管理输入输出数据的结构体

AVIODirEntry *entry =NULL;� ��AVIODirEntry 为文件信息的结构体,内部包含 文件名称,大小等信息 ls -all 下的属性

使用 avio_open_dir(&ctx,"路径",NULL); 来打开需要操作的目录路径

使用 avio_read_dir(ctx, &entry);� 来读取目录下的每个目录&文件�

使用 avio_close_dir(&ctx);� 来关闭目录 释放资源

代码:

� �

#include <stdio.h>#include <libavutil/log.h>#include <libavformat/avformat.h>int main(){ av_log_set_level(AV_LOG_DEBUG); int ret; AVIODirContext *ctx =NULL; AVIODirEntry *entry =NULL; ret = avio_open_dir(&ctx,"/home/FFmpeg",NULL); if(ret <0)  { av_log(NULL, AV_LOG_ERROR,"Cant open dir: %s\n", av_err2str(ret)); return -1;  } while(1) {ret = avio_read_dir(ctx, &entry);if(ret <0) {  av_log(NULL, AV_LOG_ERROR,"Cant read dir:%s\n", av_err2str(ret));  goto __fail;}if(!entry) {break;}av_log(NULL, AV_LOG_INFO,""PRId64" %s \n", entry->size, entry->name);//释放资源avio_free_directory_entry(&entry); } __fail:  avio_close_dir(&ctx); //关闭文件 return 0;}

�第一节:安装FFmpeg和基本Api的使用 结束

源文地址:https://www.guoxiongfei.cn/csdn/4618.html