Mp4Parser音频视频合并输出不能在所有设备上播放

2022-05-31 00:00:00 mp4 android java mp4parser mi

在我的Android应用程序中,我正在分别下载Facebook视频和音频,并将其合并到一个输出文件中。原因是Facebook视频URL没有声音,而音频URL是分开的。 我试过用这个mp4Parser来合并音频和视频,它在大多数设备上都工作得很好,但在Redmi/MI设备上播放设备文件时遇到了问题。

我遵循的步骤

  • 下载MP4格式的无声视频。
  • 下载M4a格式的音频。
  • 合并两个文件并创建MP4格式的输出文件。
  • 合并完成后,没有删除任何声音、视频和临时音频。

下面是我的代码

依赖关系

 implementation 'com.iceteck.silicompressorr:silicompressor:2.2.4'
    implementation('com.googlecode.mp4parser:isoparser:1.0.6') {
        exclude group: 'org.aspectj', module: 'aspectjrt'
    }

合并代码

  private fun mergeAudioAndVideo(
        id: Int,
        model: DatabaseDownloadItem?,
        file: File?,
        activityInstance: LocaleAwareCompatActivity?
    ) {
        val mergingDetail = sharedPreferencesManager.getItemInMergingList(id)
        if (mergingDetail != null) {
            val timeInMillis = System.currentTimeMillis()
            var list: ArrayList<DatabaseDownloadItem>? = sharedViewmodel.pendingMerge.value
            if (list == null) {
                list = arrayListOf()
            }
            list.add(model!!)
            sharedViewmodel.pendingMerge.postValue(list)
            sharedViewmodel.pendingMerge.value?.add(model!!)
            CoroutineScope(Dispatchers.Main).launch {
                var mergePendingItems: ArrayList<Int>? =
                    tinyDB?.getMergeList(AppConstants.MERGE_PENDING_DOWNLOADS)
                if (mergePendingItems == null) {
                    mergePendingItems = arrayListOf()
                }
                if (!mergePendingItems.contains(model.id)) {
                    mergePendingItems.add(model.id)
                }
                tinyDB?.putMergeList(AppConstants.MERGE_PENDING_DOWNLOADS, mergePendingItems)
                adapter.notifyDataSetChanged()
                CoroutineScope(Dispatchers.IO + exceptionHandler).async {
                    val audiopath = mergingDetail.audioPath
                    val videopath = mergingDetail.videoPath
                    /*
                    following comment is kept for reference
                    val audiopath = "/storage/emulated/0/VideoDownloader/tempAudio_1643715180488.m4a"
                    val videopath = "/storage/emulated/0/VideoDownloader/1643715180480.mp4"*/
                    val output: String
                    val outputName = "output_${mergingDetail.originalFileName}"
                    mergingDetail.outputFileName = outputName
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                        val file = File(
                            getDownloaderFolderFromDownloads(),
                            "$outputName.mp4"
                        )
                        output = file.absolutePath
                        mergingDetail.outputFile = file

                    } else {
                        val file = File(
                            Environment.getExternalStorageDirectory()
                                .toString() + "/VideoDownloader/",
                            "$outputName.mp4"
                        )
                        output = file.absolutePath
                        mergingDetail.outputFile = file

                    }
                    var videoOutput: Movie? = null
                    var audioOutput: Movie? = null
                    kotlin.runCatching {
                        videoOutput = MovieCreator.build(videopath)
                        audioOutput = MovieCreator.build(audiopath)
                    }
                    val finalTrack: MutableList<Track> = ArrayList()
                    for (track in videoOutput?.tracks!!) {
                        if (track.handler.equals("vide")) finalTrack.add(track)
                    }
                    for (track in audioOutput?.tracks!!) {
                        if (track.handler.equals("soun")) finalTrack.add(track)
                    }
                    videoOutput?.tracks = finalTrack
                    val mp4file: Container = DefaultMp4Builder().build(videoOutput)
                    kotlin.runCatching {
                        val fc: FileChannel = FileOutputStream(File(output)).getChannel()
                        mp4file.writeContainer(fc)
                        fc.close()
                    }.onFailure { error ->
                        val abc = error
                    }.onSuccess { someFuncReturnValue ->
                        val abc = "dss"
                    }

                }.await()
                Toast.makeText(activityInstance, "Audio Video Merging Done", Toast.LENGTH_SHORT)
                    .show()
                mergingDetail.isMergingDone = true
                sharedPreferencesManager.updateItemInMergingList(id, mergingDetail)
                executeDownloadCompletion(model, file, activityInstance)
                deleteTempAudioFile(File(mergingDetail.audioPath!!), activityInstance)
                deleteOldMutedVideo(File(mergingDetail.videoPath!!), activityInstance)
                renameOutputFileWithDBName(mergingDetail, activityInstance)
                var list: ArrayList<DatabaseDownloadItem>? = sharedViewmodel.pendingMerge.value
                if (list != null && list!!.size > 0) {
                    list!!.remove(model!!)
                    sharedViewmodel.pendingMerge.postValue(list)
                    sharedViewmodel.pendingMerge.value?.add(model!!)
                }
                notificationWork(model, file, activityInstance)
                sharedPreferencesManager.deleteItemInMergingList(id)

            }
        }
    }

这在大多数设备中都工作得很好,但在MI设备(即Redmi S2,MUI版本MUI Global 12.0.2)中,我在播放输出文件时遇到问题,文件已损坏,无法播放。显示的错误只是标题未知。

请注意我还尝试了以下依赖项组合,但这会在输出文件中生成滞后,因此删除了这两个项。

implementation 'org.mp4parser:isoparser:1.9.41'
     implementation 'org.mp4parser:muxer:1.9.41

谁能推荐一下mp4parser用于合并音频文件和视频文件的正确usgae和依赖项版本。

如有任何帮助,将不胜感激。

谢谢


解决方案

使用FFmpeg库,它将完成所有工作。您只需提供一个Facebook音频和视频链接和您的目录的存储路径,您将通过文件API获得。它将下载音频和视频,然后将它们合并。

这个简单的命令将实现所有的魔力。

val cmd = "-i $videoUrl -i  $audioUrl -c:v copy -c:a aac $storagePath"
FFmpeg.executeAsync(cmd, object: ExecuteCallback())

相关文章