Skip to content

Commit 782a69f

Browse files
committed
🎨 新增ffmpeg config配置
1 parent 6959cdf commit 782a69f

File tree

3 files changed

+137
-15
lines changed

3 files changed

+137
-15
lines changed

ffmpeg/src/main/java/com/coder/ffmpeg/jni/FFmpegCmd.kt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.coder.ffmpeg.jni
22

3-
import android.util.Log
43
import com.coder.ffmpeg.annotation.CodecAttribute
54
import com.coder.ffmpeg.annotation.CodecProperty
65
import com.coder.ffmpeg.annotation.FormatAttribute
@@ -39,7 +38,9 @@ internal class FFmpegCmd private constructor() {
3938
/**
4039
* Whether to enable debugging mode
4140
* @param debug true or false
41+
* you can see [FFmpegConfig.setDebug]
4242
*/
43+
@Deprecated("delete")
4344
external fun setDebug(debug: Boolean)
4445

4546
/**
@@ -101,13 +102,6 @@ internal class FFmpegCmd private constructor() {
101102
return if (ffdebug) cmds else cmd
102103
}
103104

104-
/**
105-
* Execute ffmpeg command method
106-
* @param command ffmeng command
107-
* @return execute status
108-
*/
109-
private external fun run(command: String?): Int
110-
111105
/**
112106
* Execute ffmpeg command method
113107
* @param command ffmeng command
@@ -131,20 +125,20 @@ internal class FFmpegCmd private constructor() {
131125
* @param type information type.
132126
*/
133127
private external fun info(videoPath: String?, type: Int): Int
128+
129+
/**
130+
* Call native to get media information.
131+
* @param videoPath media path
132+
* @param type information type.
133+
*/
134+
private external fun codec(videoPath: String?, type: Int): CodecInfo?
134135
/**
135136
* Provide method to get codec info .
136137
* @param property property type.
137138
*/
138139
fun getCodecProperty(videoPath: String?,@CodecProperty property: Int): CodecInfo? {
139140
return codec(videoPath, property)
140141
}
141-
/**
142-
* Call native to get media information.
143-
* @param videoPath media path
144-
* @param type information type.
145-
*/
146-
private external fun codec(videoPath: String?, type: Int): CodecInfo?
147-
148142
/**
149143
* Provide method to get format info .
150144
* @param format format type.

ffmpeg/src/main/java/com/coder/ffmpeg/jni/FFmpegCommand.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ object FFmpegCommand {
1616
* Whether to enable debug mode
1717
*
1818
* @param debug true:enable false :not enable
19+
* you can see [FFmpegConfig.setDebug]
1920
*/
21+
@Deprecated("the method is deprecated", ReplaceWith("FFmpegConfig.setDebug(debug)"))
2022
@JvmStatic
2123
fun setDebug(debug: Boolean) {
2224
FFmpegCmd.instance?.setDebug(debug)
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.coder.ffmpeg.jni
2+
3+
import android.content.Context
4+
import java.io.File
5+
import java.io.FileOutputStream
6+
import java.io.IOException
7+
import java.util.Collections
8+
import java.util.concurrent.atomic.AtomicReference
9+
10+
11+
class FFmpegConfig {
12+
13+
companion object {
14+
15+
init {
16+
System.loadLibrary("ffmpeg-org")
17+
System.loadLibrary("ffmpeg-command")
18+
}
19+
20+
/**
21+
* Set the env of native
22+
* @param name env name
23+
* @param value env value
24+
*/
25+
private external fun setNativeEnv(name: String, value: String)
26+
27+
/**
28+
* Whether to enable debugging mode
29+
* @param debug true or false
30+
*/
31+
external fun setDebug(debug: Boolean)
32+
33+
/**
34+
* Set font config dir for fontconfig
35+
* Note:It's a config dir not font dir
36+
* @param configPath the font config dir
37+
*/
38+
fun setFontConfigPath(configPath: String) {
39+
setNativeEnv("FONTCONFIG_PATH", configPath)
40+
}
41+
42+
/**
43+
* Set font config file for fontconfig
44+
* Note:It's a config file not font file
45+
* @param configFile the font config file
46+
*/
47+
fun setFontConfigFile(configFile: String) {
48+
setNativeEnv("FONTCONFIG_FILE", configFile)
49+
}
50+
51+
/**
52+
* Set font dir for fontconfig
53+
* @param context context for application
54+
* @param fontDir the font dir contain fonts (.ttf and .otf files)
55+
* @param fontNameMapping
56+
*/
57+
fun setFontDir(context: Context, fontDir:String, fontNameMapping: Map<String, String>){
58+
setFontDirList(context, Collections.singletonList(fontDir),fontNameMapping)
59+
}
60+
/**
61+
* Set font dir for fontconfig
62+
* @param context context for application
63+
* @param fontDirList list of directories that contain fonts (.ttf and .otf files)
64+
*/
65+
fun setFontDirList(context: Context, fontDirList: List<String>, fontNameMapping: Map<String, String>) {
66+
var validFontNameMappingCount = 0
67+
val cacheDir = context.cacheDir
68+
val fontConfigDir = File(cacheDir, "fontconfig")
69+
if (!fontConfigDir.exists()) {
70+
fontConfigDir.mkdirs()
71+
}
72+
// Create font config
73+
val fontConfigFile = File(fontConfigDir, "fonts.conf")
74+
if (fontConfigFile.exists() && fontConfigFile.isFile) {
75+
fontConfigFile.delete()
76+
}
77+
fontConfigFile.createNewFile()
78+
val fontNameMappingBlock = StringBuilder()
79+
if (fontNameMapping.isNotEmpty()){
80+
for (entry in fontNameMapping.entries) {
81+
val fontName: String = entry.key
82+
val mappedFontName: String = entry.value
83+
84+
if ((fontName.trim().isNotEmpty()) && (mappedFontName.trim().isNotEmpty())) {
85+
fontNameMappingBlock.append(" <match target=\"pattern\">\n");
86+
fontNameMappingBlock.append(" <test qual=\"any\" name=\"family\">\n");
87+
fontNameMappingBlock.append(String.format(" <string>%s</string>\n", fontName));
88+
fontNameMappingBlock.append(" </test>\n");
89+
fontNameMappingBlock.append(" <edit name=\"family\" mode=\"assign\" binding=\"same\">\n");
90+
fontNameMappingBlock.append(String.format(" <string>%s</string>\n", mappedFontName));
91+
fontNameMappingBlock.append(" </edit>\n");
92+
fontNameMappingBlock.append(" </match>\n");
93+
94+
validFontNameMappingCount++
95+
}
96+
}
97+
}
98+
val fontConfigBuilder = StringBuilder()
99+
fontConfigBuilder.append("<?xml version=\"1.0\"?>\n")
100+
fontConfigBuilder.append("<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n")
101+
fontConfigBuilder.append("<fontconfig>\n")
102+
fontConfigBuilder.append(" <dir prefix=\"cwd\">.</dir>\n")
103+
for (fontDirectoryPath in fontDirList) {
104+
fontConfigBuilder.append(" <dir>")
105+
fontConfigBuilder.append(fontDirectoryPath)
106+
fontConfigBuilder.append("</dir>\n")
107+
}
108+
fontConfigBuilder.append(fontNameMappingBlock)
109+
fontConfigBuilder.append("</fontconfig>\n")
110+
val reference = AtomicReference<FileOutputStream>()
111+
try {
112+
val outputStream = FileOutputStream(fontConfigFile)
113+
reference.set(outputStream)
114+
115+
outputStream.write(fontConfigBuilder.toString().toByteArray())
116+
outputStream.flush()
117+
setFontConfigPath(fontConfigDir.absolutePath)
118+
}catch (e:IOException){
119+
e.printStackTrace()
120+
}finally {
121+
reference.get().close()
122+
}
123+
}
124+
125+
}
126+
}

0 commit comments

Comments
 (0)