Skip to content

Commit

Permalink
🎨 新增ffmpeg config配置
Browse files Browse the repository at this point in the history
  • Loading branch information
AnJoiner committed Nov 10, 2023
1 parent 6959cdf commit 782a69f
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 15 deletions.
24 changes: 9 additions & 15 deletions ffmpeg/src/main/java/com/coder/ffmpeg/jni/FFmpegCmd.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.coder.ffmpeg.jni

import android.util.Log
import com.coder.ffmpeg.annotation.CodecAttribute
import com.coder.ffmpeg.annotation.CodecProperty
import com.coder.ffmpeg.annotation.FormatAttribute
Expand Down Expand Up @@ -39,7 +38,9 @@ internal class FFmpegCmd private constructor() {
/**
* Whether to enable debugging mode
* @param debug true or false
* you can see [FFmpegConfig.setDebug]
*/
@Deprecated("delete")
external fun setDebug(debug: Boolean)

/**
Expand Down Expand Up @@ -101,13 +102,6 @@ internal class FFmpegCmd private constructor() {
return if (ffdebug) cmds else cmd
}

/**
* Execute ffmpeg command method
* @param command ffmeng command
* @return execute status
*/
private external fun run(command: String?): Int

/**
* Execute ffmpeg command method
* @param command ffmeng command
Expand All @@ -131,20 +125,20 @@ internal class FFmpegCmd private constructor() {
* @param type information type.
*/
private external fun info(videoPath: String?, type: Int): Int

/**
* Call native to get media information.
* @param videoPath media path
* @param type information type.
*/
private external fun codec(videoPath: String?, type: Int): CodecInfo?
/**
* Provide method to get codec info .
* @param property property type.
*/
fun getCodecProperty(videoPath: String?,@CodecProperty property: Int): CodecInfo? {
return codec(videoPath, property)
}
/**
* Call native to get media information.
* @param videoPath media path
* @param type information type.
*/
private external fun codec(videoPath: String?, type: Int): CodecInfo?

/**
* Provide method to get format info .
* @param format format type.
Expand Down
2 changes: 2 additions & 0 deletions ffmpeg/src/main/java/com/coder/ffmpeg/jni/FFmpegCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ object FFmpegCommand {
* Whether to enable debug mode
*
* @param debug true:enable false :not enable
* you can see [FFmpegConfig.setDebug]
*/
@Deprecated("the method is deprecated", ReplaceWith("FFmpegConfig.setDebug(debug)"))
@JvmStatic
fun setDebug(debug: Boolean) {
FFmpegCmd.instance?.setDebug(debug)
Expand Down
126 changes: 126 additions & 0 deletions ffmpeg/src/main/java/com/coder/ffmpeg/jni/FFmpegConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.coder.ffmpeg.jni

import android.content.Context
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.Collections
import java.util.concurrent.atomic.AtomicReference


class FFmpegConfig {

companion object {

init {
System.loadLibrary("ffmpeg-org")
System.loadLibrary("ffmpeg-command")
}

/**
* Set the env of native
* @param name env name
* @param value env value
*/
private external fun setNativeEnv(name: String, value: String)

/**
* Whether to enable debugging mode
* @param debug true or false
*/
external fun setDebug(debug: Boolean)

/**
* Set font config dir for fontconfig
* Note:It's a config dir not font dir
* @param configPath the font config dir
*/
fun setFontConfigPath(configPath: String) {
setNativeEnv("FONTCONFIG_PATH", configPath)
}

/**
* Set font config file for fontconfig
* Note:It's a config file not font file
* @param configFile the font config file
*/
fun setFontConfigFile(configFile: String) {
setNativeEnv("FONTCONFIG_FILE", configFile)
}

/**
* Set font dir for fontconfig
* @param context context for application
* @param fontDir the font dir contain fonts (.ttf and .otf files)
* @param fontNameMapping
*/
fun setFontDir(context: Context, fontDir:String, fontNameMapping: Map<String, String>){
setFontDirList(context, Collections.singletonList(fontDir),fontNameMapping)
}
/**
* Set font dir for fontconfig
* @param context context for application
* @param fontDirList list of directories that contain fonts (.ttf and .otf files)
*/
fun setFontDirList(context: Context, fontDirList: List<String>, fontNameMapping: Map<String, String>) {
var validFontNameMappingCount = 0
val cacheDir = context.cacheDir
val fontConfigDir = File(cacheDir, "fontconfig")
if (!fontConfigDir.exists()) {
fontConfigDir.mkdirs()
}
// Create font config
val fontConfigFile = File(fontConfigDir, "fonts.conf")
if (fontConfigFile.exists() && fontConfigFile.isFile) {
fontConfigFile.delete()
}
fontConfigFile.createNewFile()
val fontNameMappingBlock = StringBuilder()
if (fontNameMapping.isNotEmpty()){
for (entry in fontNameMapping.entries) {
val fontName: String = entry.key
val mappedFontName: String = entry.value

if ((fontName.trim().isNotEmpty()) && (mappedFontName.trim().isNotEmpty())) {
fontNameMappingBlock.append(" <match target=\"pattern\">\n");
fontNameMappingBlock.append(" <test qual=\"any\" name=\"family\">\n");
fontNameMappingBlock.append(String.format(" <string>%s</string>\n", fontName));
fontNameMappingBlock.append(" </test>\n");
fontNameMappingBlock.append(" <edit name=\"family\" mode=\"assign\" binding=\"same\">\n");
fontNameMappingBlock.append(String.format(" <string>%s</string>\n", mappedFontName));
fontNameMappingBlock.append(" </edit>\n");
fontNameMappingBlock.append(" </match>\n");

validFontNameMappingCount++
}
}
}
val fontConfigBuilder = StringBuilder()
fontConfigBuilder.append("<?xml version=\"1.0\"?>\n")
fontConfigBuilder.append("<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n")
fontConfigBuilder.append("<fontconfig>\n")
fontConfigBuilder.append(" <dir prefix=\"cwd\">.</dir>\n")
for (fontDirectoryPath in fontDirList) {
fontConfigBuilder.append(" <dir>")
fontConfigBuilder.append(fontDirectoryPath)
fontConfigBuilder.append("</dir>\n")
}
fontConfigBuilder.append(fontNameMappingBlock)
fontConfigBuilder.append("</fontconfig>\n")
val reference = AtomicReference<FileOutputStream>()
try {
val outputStream = FileOutputStream(fontConfigFile)
reference.set(outputStream)

outputStream.write(fontConfigBuilder.toString().toByteArray())
outputStream.flush()
setFontConfigPath(fontConfigDir.absolutePath)
}catch (e:IOException){
e.printStackTrace()
}finally {
reference.get().close()
}
}

}
}

0 comments on commit 782a69f

Please sign in to comment.