home archives github knives links
tags ffmpeg opencv ndk
categories
only title title and content
ffmpeg笔记

ubuntu安装

./configure   --enable-shared  --prefix=/usr/local/ffmpeg  --enable-gpl --enable-libx264  --enable-libx265
make -j4 && make install

Android端静态ffmpeg库与OpenCV交叉编译

参考:CSDN

编写ffmpeg的编译脚本

TOOLCHAIN=/home/lynx/fuck_mount/AndroidSDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64

./configure \
--prefix=/home/lynx/fuck_mount/ffmpeg/android_static \
--enable-avresample \
--disable-shared \
--enable-static \
--disable-programs \
--disable-doc \
--disable-avdevice \
--disable-postproc \
--target-os=android \
--arch=aarch64 \
--enable-cross-compile \
--cross-prefix=$TOOLCHAIN/bin/aarch64-linux-android- \
--cc=$TOOLCHAIN/bin/aarch64-linux-android30-clang \
--cxx=$TOOLCHAIN/bin/aarch64-linux-android30-clang++ \
--sysroot=$TOOLCHAIN/sysroot

修改openCV的cmake文件

  1. /CmakeLists.txt
# OCV_OPTION(WITH_FFMPEG "Include FFMPEG support" (NOT ANDROID)
OCV_OPTION(WITH_FFMPEG "Include FFMPEG support" (ON)
VISIBLE_IF NOT IOS AND NOT WINRT
VERIFY HAVE_FFMPEG)

常见错误

  1. 参数冲突导致的错误
    /usr/bin/ld: unknown architecture of input file `libavutil/log2_tab.o' is incompatible with i386:x86-64 output
    /usr/bin/ld: unknown architecture of input file `libavutil/reverse.o' is incompatible with i386:x86-64 output

直接删掉整个源代码文件夹, 重新下载新的源代码进行编译

Android端动态ffmpeg库与OpenCV交叉编译

编写ffmpeg的编译脚本

参考:CSDN

TOOLCHAIN=/home/lynx/fuck_mount/AndroidSDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64

./configure \
--target-os=android \
--prefix=/home/lynx/fuck_mount/ffmpeg/android_shared \
--enable-cross-compile --enable-runtime-cpudetect \
--enable-jni --enable-mediacodec --enable-avresample \
--disable-asm --disable-x86asm --disable-network --disable-doc \
--disable-stripping --disable-ffprobe --disable-ffplay --disable-debug \
--enable-gpl --enable-shared \
--arch=aarch64 \
--cross-prefix=$TOOLCHAIN/bin/aarch64-linux-android- \
--cc=$TOOLCHAIN/bin/aarch64-linux-android30-clang \
--cxx=$TOOLCHAIN/bin/aarch64-linux-android30-clang++ \
--nm=$TOOLCHAIN/bin/aarch64-linux-android-nm \
--sysroot=$TOOLCHAIN/sysroot \
--extra-cflags="-fPIC -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv8-a"

命令

视频转换

gif转视频

ffmpeg -f gif -i draw1.gif draw1.mpg

视频合并

ffmpeg -i "concat:draw1.mpg|draw2.mpg|pen.mpg|edit.mpg|clip.mpg" demo.mp4

合并音视频(添加音轨)

ffmpeg -i input.aac -i input.mp4 output.mp4

查看文件信息

ffmpeg -i test.mp4

视频剪切

# 指定时长
ffmpeg -i input.mp4 -ss 00:46:26 -t 00:57:45 -c copy output.mp4
# 指定结束时间
ffmpeg -i input.mp4 -ss 00:46:26 -to 00:57:45 -c copy output.mp4

视频截图

# 截取单帧
ffmpeg -i input.mp4 -ss 00:00:00 -vframes 1 output.jpg
# 固定时间开始连续截取多张
ffmpeg -i input.mp4 -ss 00:00:00 -vframes 10 output.jpg

质量调整

# 码率
ffmpeg -i input.mp4 -b 1.5M output.mp4
# 帧率(fps)
ffmpeg -i input.mp4 -r 25 output.mp4
# 分辨率, 必须是2的整数倍
ffmpeg -i input.mp4 -vf scale=400:600 output.mp4 # 保持比例
ffmpeg -i input.mp4 -vf scale=400:-2 output.mp4
# 直接控制输出文件大小, 有bug, 视频的时长不对
ffmpeg -i input.mp4 -fs 10000k output.mp4

格式转换

# 视频格式转换
ffmpeg -i input.mp4 output.avi

图像分辨率修改

参考

# 指定长宽
ffmpeg -i input.jpg -vf scale=320:240 output.png
# 等比例缩放
ffmpeg -i input.jpg -vf scale=320:-1 output.png
# 倍数缩放
ffmpeg -i input.jpg -vf scale=iw*2:ih output.png
ffmpeg -i input.jpg -vf "scale=iw*.5:ih*.5" output.png
ffmpeg -i input.jpg -vf "scale=iw/2:ih/2" output.png

提取音频

# 提取完整音频
ffmpeg -i test.mp4 -q:a 0 -map a test.mp3
# 只提取音频流而不重新编码(不能是mp3格式, 只能是aac或m4a)
ffmpeg -i test.mp4 -vn -acodec copy test.m4a

倒放

# 视频倒放, 音频不变
ffmpeg -i input.mp4 -vf reverse output.mp4
# 音频倒放, 视频不变
ffmpeg -i input.mp4 -map 0 -c:v copy -af areverse output.mp4
# 音视频倒放
ffmpeg -i input.mp4 -vf reverse -af areverse -preset superfast output.mp4

旋转与翻转

# 水平翻转
ffmpeg -i input.mp4 -vf "hflip" output.mp4
# 垂直翻转
ffmpeg -i input.mp4 -vf "vflip" output.mp4
# 逆时针旋转90度并垂直翻转
ffmpeg -i input.mp4 -vf "transpose=0" output.mp4
# 顺时针旋转90度
ffmpeg -i input.mp4 -vf "transpose=1" output.mp4
# 逆时针旋转90度
ffmpeg -i input.mp4 -vf "transpose=2" output.mp4
# 顺时针旋转90度并垂直翻转
ffmpeg -i input.mp4 -vf "transpose=3" output.mp4
# 自定义角度, 但是图像的尺寸不会变, 会有黑边
ffmpeg -i input.mp4 -vf "rotate=PI/2" output.mp4

裁剪图片

ffmpeg -i input.jpg -vf crop=width:height:from_x:from_y output.jpg

常见错误

  1. Default encoder for format mp3 (codec mp3) is probably disabled. Please choose an encoder manually.(TODO)
    1. 先安装libmp3lame-dev
    2. 再重新编译ffmpeg: 添加--enable-libmp3lame参数