Safer usage of FFmpegKit

This commit is contained in:
Matthieu 2022-08-21 00:31:36 +02:00
parent 9da78746fd
commit 026374843e
3 changed files with 18 additions and 16 deletions

View File

@ -39,7 +39,7 @@ import org.pixeldroid.app.postCreation.photoEdit.VideoEditActivity
import org.pixeldroid.app.utils.BaseThemedWithoutBarActivity
import org.pixeldroid.app.utils.db.entities.InstanceDatabaseEntity
import org.pixeldroid.app.utils.db.entities.UserDatabaseEntity
import org.pixeldroid.app.utils.ffmpegSafeUri
import org.pixeldroid.app.utils.ffmpegCompliantUri
import org.pixeldroid.app.utils.fileExtension
import org.pixeldroid.app.utils.getMimeType
import java.io.File
@ -333,21 +333,22 @@ class PostCreationActivity : BaseThemedWithoutBarActivity() {
//val file = File.createTempFile("temp_video", ".webm")
model.trackTempFile(file)
val fileUri = file.toUri()
val outputVideoPath = ffmpegSafeUri(fileUri)
val outputVideoPath = ffmpegCompliantUri(fileUri)
val inputUri = model.getPhotoData().value!![position].imageUri
val inputSafePath = ffmpegSafeUri(inputUri)
val ffmpegCompliantUri: String = ffmpegCompliantUri(inputUri)
val mediaInformation: MediaInformation? = FFprobeKit.getMediaInformation(ffmpegSafeUri(inputUri)).mediaInformation
val mediaInformation: MediaInformation? = FFprobeKit.getMediaInformation(ffmpegCompliantUri(inputUri)).mediaInformation
val totalVideoDuration = mediaInformation?.duration?.toFloatOrNull()
val mutedString = if(muted) "-an" else ""
val startString = if(videoStart != null) "-ss $videoStart" else ""
val mutedString = if(muted) "-an" else null
val startString: List<String?> = if(videoStart != null) listOf("-ss", "$videoStart") else listOf(null, null)
val endString = if(videoEnd != null) "-to ${videoEnd - (videoStart ?: 0f)}" else ""
val endString: List<String?> = if(videoEnd != null) listOf("-to", "${videoEnd - (videoStart ?: 0f)}") else listOf(null, null)
val session: FFmpegSession = FFmpegKit.executeAsync("$startString -i $inputSafePath $endString -c copy $mutedString -y $outputVideoPath",
val session: FFmpegSession =
FFmpegKit.executeWithArgumentsAsync(listOfNotNull(startString[0], startString[1], "-i", ffmpegCompliantUri, endString[0], endString[1], "-c", "copy", mutedString, "-y", outputVideoPath).toTypedArray(),
//val session: FFmpegSession = FFmpegKit.executeAsync("$startString -i $inputSafePath $endString -c:v libvpx-vp9 -c:a copy -an -y $outputVideoPath",
{ session ->
val returnCode = session.returnCode

View File

@ -29,7 +29,7 @@ import org.pixeldroid.app.databinding.ActivityVideoEditBinding
import org.pixeldroid.app.postCreation.PostCreationActivity
import org.pixeldroid.app.postCreation.carousel.dpToPx
import org.pixeldroid.app.utils.BaseThemedWithBarActivity
import org.pixeldroid.app.utils.ffmpegSafeUri
import org.pixeldroid.app.utils.ffmpegCompliantUri
import java.io.File
@ -61,7 +61,7 @@ class VideoEditActivity : BaseThemedWithBarActivity() {
val uri = intent.getParcelableExtra<Uri>(PhotoEditActivity.PICTURE_URI)!!
videoPosition = intent.getIntExtra(PhotoEditActivity.PICTURE_POSITION, -1)
val inputVideoPath = ffmpegSafeUri(uri)
val inputVideoPath = ffmpegCompliantUri(uri)
val mediaInformation: MediaInformation? = FFprobeKit.getMediaInformation(inputVideoPath).mediaInformation
binding.muter.setOnClickListener {
@ -232,15 +232,16 @@ class VideoEditActivity : BaseThemedWithBarActivity() {
val file = File.createTempFile("temp_img", ".bmp")
tempFiles.add(file)
val fileUri = file.toUri()
val inputSafePath = ffmpegSafeUri(inputUri)
val ffmpegCompliantUri = ffmpegCompliantUri(inputUri)
val outputImagePath =
if(fileUri.toString().startsWith("content://"))
FFmpegKitConfig.getSafParameterForWrite(this, fileUri)
else fileUri.toString()
val session = FFmpegKit.executeAsync(
"-noaccurate_seek -ss $thumbTime -i $inputSafePath -vf scale=${thumbnail.width}:${thumbnail.height} -frames:v 1 -f image2 -y $outputImagePath",
{ session ->
val session = FFmpegKit.executeWithArgumentsAsync(arrayOf(
"-noaccurate_seek", "-ss", "$thumbTime", "-i", ffmpegCompliantUri, "-vf",
"scale=${thumbnail.width}:${thumbnail.height}",
"-frames:v", "1", "-f", "image2", "-y", outputImagePath), { session ->
val state = session.state
val returnCode = session.returnCode
@ -254,7 +255,7 @@ class VideoEditActivity : BaseThemedWithBarActivity() {
// CALLED WHEN SESSION IS EXECUTED
Log.d("VideoEditActivity", "FFmpeg process exited with state $state and rc $returnCode.${session.failStackTrace}")
},
{/* CALLED WHEN SESSION PRINTS LOGS */ }) { /*CALLED WHEN SESSION GENERATES STATISTICS*/ }
{/* CALLED WHEN SESSION PRINTS LOGS */ }, { /*CALLED WHEN SESSION GENERATES STATISTICS*/ })
sessionList.add(session.sessionId)
}

View File

@ -96,7 +96,7 @@ fun normalizeDomain(domain: String): String {
.trim(Char::isWhitespace)
}
fun Context.ffmpegSafeUri(inputUri: Uri?): String =
fun Context.ffmpegCompliantUri(inputUri: Uri?): String =
if (inputUri?.scheme == "content")
FFmpegKitConfig.getSafParameterForRead(this, inputUri)
else inputUri.toString()