您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
3. avformat_open_input函数详解并示例打开mp4文件~1
发布时间:2021-05-30 10:37:10编辑:雪饮阅读()
Ffmpeg的avformat_open_input函数用于打开多媒体数据并且获取一些信息。
它的声明位于libavformat/avformat.h
这里就以这个函数为例,实现打开一个视频文件
那么我们就需要用到头文件libavformat/avformat.h,另外就是我们希望不要有太多其它代码,这里就将native-lib.cpp中之前的open方法干掉,只用默认的给我们生成的stringFromJNI方法来实验,于是乎cpp/native-lib.cpp:
#include <jni.h>
#include <string>
#include <android/log.h>
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,"testff",__VA_ARGS__)
extern "C"{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_ndk_1and_141_MainActivity_stringFromJNI(JNIEnv* env,jobject) {
std::string hello = "Hello from C++";
hello+=avcodec_configuration();
//初始化解封裝
av_register_all();
//初始化網絡
avformat_network_init();
AVFormatContext *ic=NULL;
char path[]="/sdcard/1080.mp4";
int re=avformat_open_input(&ic,path,0,0);
if(re==0){
LOGW("avformat_open_input %s success!");
}
else{
LOGW("avformat_open_input failed!:%s",av_err2str(re));
}
return env->NewStringUTF(hello.c_str());
}
#include <string>
#include <android/log.h>
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,"testff",__VA_ARGS__)
extern "C"{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_ndk_1and_141_MainActivity_stringFromJNI(JNIEnv* env,jobject) {
std::string hello = "Hello from C++";
hello+=avcodec_configuration();
//初始化解封裝
av_register_all();
//初始化網絡
avformat_network_init();
AVFormatContext *ic=NULL;
char path[]="/sdcard/1080.mp4";
int re=avformat_open_input(&ic,path,0,0);
if(re==0){
LOGW("avformat_open_input %s success!");
}
else{
LOGW("avformat_open_input failed!:%s",av_err2str(re));
}
return env->NewStringUTF(hello.c_str());
}
那么同样的MainActivity.java中也要清除掉之前的open的native方法,并做了些稍微调整后的MainActivity.java:如:
package com.example.ndk_and_41;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
那么对于本次实例,我们还需要用到两个动态链接库libavformat.so、libavutil.so于是乎
cpp/CMakeLists.txt调整如:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("ndk_and_41")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#添加頭文件路徑(括號中的include是相對於本文件路徑)
include_directories(../../../include)
#設置ffmpeg庫所在路徑的變量,這裏的FF是自定義的一個名字
set(FF ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI})
#avcodec這個是自定義的一個名字
add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavcodec.so)
add_library(avformat SHARED IMPORTED)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${FF}/libavformat.so)
add_library(avutil SHARED IMPORTED)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${FF}/libavutil.so)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
avcodec avformat avutil
#這裏將avcodec庫鏈接進來
# Links the target library to the log library
# included in the NDK.
${log-lib} )
那么此时可以编译并部署到设备了吗?这里以魅族16T为例
可以发现在build过程中都已经出现了问题,这里就更不必等着去看install之后的效果了,肯定是失败输出。
那么如何解决这个问题呢?
这里就需要引入昨天咱们新写的那个build_android_64.sh脚本所编译出来的动态链接库。
那么这里我们在我们这个安卓项目中app/libs下面建立arm64-v8a目录
然后将昨天编译输出的64位动态链接库目录\\192.168.1.101\root\ff\ffmpeg-3.4\android\arm64-neon-hard\lib中的所有.so文件(即排除该目录中的pkgconfig文件夹以外的文件)都拷贝到刚刚新建的这个arm64-v8a目录中。
然后和前面一样,在我们这个安卓项目app/build.gradle中还需要对arm64-v8a架构的cpu对应动态链接库进行相关处理,则build.gradle:
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.ndk_and_41"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
ndk{
abiFilters "armeabi-v7a"
abiFilters "arm64-v8a"
}
}
sourceSets{
main{
jniLibs.srcDirs=['libs']
}
}
}
packagingOptions {
pickFirst 'lib/armeabi-v7a/libavcodec.so'
pickFirst 'lib/armeabi-v7a/libavutil.so'
pickFirst 'lib/armeabi-v7a/libavformat.so'
pickFirst 'lib/arm64-v8a/libavcodec.so'
pickFirst 'lib/arm64-v8a/libavutil.so'
pickFirst 'lib/arm64-v8a/libavformat.so'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
最后编译部署运行到魅族16T中,则可以看到成功的打开多媒体数据并且获取一些信息。
日志猫中的信息如:
2021-05-30 10:15:38.181 15696-15696/com.example.ndk_and_41 W/testff: avformat_open_input success!
关键字词:avformat_open_input,64位,动态链接库