如何将FFmpeg导入Android项目

问题描述 投票:0回答:0

我正在尝试将一系列图像转换为视频。 为此,我将 FFmpeg 添加到我的 Android 项目中。 但是它没有正确添加,所以我无法将类 FFmpeg 导入到代码中。 在语句“int rc = FFmpeg.execute(command);”中FFmpeg 以红色突出显示。

我认为图书馆有问题。可以请你看一下吗?

我的清单文件

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.TimelapseLite"
    tools:targetApi="31">
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我的settings.gradle文件

pluginManagement {
repositories {
    google()
    mavenCentral()
    gradlePluginPortal()
}

}

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
    google()
    mavenCentral()
}

} rootProject.name = "TimelapseLite" 包括“:应用程序”

我的build.gradle文件

plugins {
    id 'com.android.application'
}

android {
    namespace 'ae.vpdev.timelapselite'
    compileSdk 33

defaultConfig {
    applicationId "ae.vpdev.timelapselite"
    minSdk 26
    targetSdk 33
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

}

依赖项{

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.arthenica:mobile-ffmpeg-full:4.5.LTS'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

}

我在MainActivity中的代码

private void convertImagesToVideo() {
    StringBuilder imageListFileContent = new StringBuilder();
    for (Uri imageUri : selectedImages) {
        imageListFileContent.append("file '").append(imageUri.getPath()).append("'\n");
    }

    try {
        File imageListFile = new File(getCacheDir(), "image_list.txt");
        FileWriter writer = new FileWriter(imageListFile);
        writer.append(imageListFileContent.toString());
        writer.flush();
        writer.close();

        File outputFile = new File(getExternalFilesDir(null), "output_video.mp4");
        String outputFilePath = outputFile.getAbsolutePath();

        String command = "-f concat -safe 0 -i " + imageListFile.getAbsolutePath() +
                " -vf \"scale=-2:720\" -r 30 -c:v libx264 -pix_fmt yuv420p " + outputFilePath;

        int rc = FFmpeg.execute(command);

        if (rc == RETURN_CODE_SUCCESS) {
            Log.d("FFmpeg", "Video conversion completed successfully");
            // Now you can use the outputFilePath to play or share the video
        } else {
            Log.d("FFmpeg", "Video conversion failed");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
java android gradle ffmpeg repository
© www.soinside.com 2019 - 2024. All rights reserved.