add subtitle into video

pull/166/head
xufulong 4 years ago
parent 89073b92c8
commit ec053549b1
  1. 24
      app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.kt
  2. 40
      app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java
  3. 7
      app/src/main/res/layout/activity_video_handle.xml
  4. 1
      app/src/main/res/values-en/strings.xml
  5. 1
      app/src/main/res/values/strings.xml

@ -117,7 +117,8 @@ class VideoHandleActivity : BaseActivity() {
R.id.btn_moov,
R.id.btn_speed,
R.id.btn_flv,
R.id.btn_thumbnail
R.id.btn_thumbnail,
R.id.btn_add_subtitle
)
}
@ -148,6 +149,10 @@ class VideoHandleActivity : BaseActivity() {
showToast(getString(R.string.wrong_video_format))
return
}
val suffix = FileUtil.getFileSuffix(srcFile)
if (suffix == null || suffix.isEmpty()) {
return
}
when (viewId) {
R.id.btn_video_transform//transform format
-> {
@ -156,10 +161,6 @@ class VideoHandleActivity : BaseActivity() {
}
R.id.btn_video_cut//cut video
-> {
val suffix = FileUtil.getFileSuffix(srcFile)
if (suffix == null || suffix.isEmpty()) {
return
}
val cutVideo = PATH + File.separator + "cutVideo" + suffix
val startTime = 0
val duration = 20
@ -321,14 +322,17 @@ class VideoHandleActivity : BaseActivity() {
}
R.id.btn_thumbnail// insert thumbnail into video
-> {
val thumbSuffix = FileUtil.getFileSuffix(srcFile)
if (thumbSuffix == null || thumbSuffix.isEmpty()) {
return
}
val thumbnailPath = PATH + File.separator + "thumb.jpg"
val thumbVideoPath = PATH + File.separator + "thumbnailVideo" + thumbSuffix
val thumbVideoPath = PATH + File.separator + "thumbnailVideo" + suffix
commandLine = FFmpegUtil.insertPicIntoVideo(srcFile, thumbnailPath, thumbVideoPath)
}
R.id.btn_add_subtitle//add subtitle into video
-> {
Log.e(TAG, "add subtitle...")
val subtitlePath = PATH + File.separator + "test.ass"
val addSubtitlePath = PATH + File.separator + "subtitle" + suffix
commandLine = FFmpegUtil.addSubtitleIntoVideo(srcFile, subtitlePath, addSubtitlePath)
}
else -> {
}
}

@ -95,14 +95,14 @@ public class FFmpegUtil {
* @return mux success or not
*/
public static String[] mediaMux(String videoFile, String audioFile, boolean copy, String muxFile) {
String mixAudioCmd;
String mediaMuxCmd;
if (copy) {
mixAudioCmd = "ffmpeg -i %s -i %s -codec copy -y %s";
mediaMuxCmd = "ffmpeg -i %s -i %s -codec copy -y %s";
} else {
mixAudioCmd = "ffmpeg -i %s -i %s -y %s";
mediaMuxCmd = "ffmpeg -i %s -i %s -y %s";
}
mixAudioCmd = String.format(Locale.getDefault(), mixAudioCmd, videoFile, audioFile, muxFile);
return mixAudioCmd.split(" ");
mediaMuxCmd = String.format(Locale.getDefault(), mediaMuxCmd, videoFile, audioFile, muxFile);
return mediaMuxCmd.split(" ");
}
/**
@ -114,9 +114,9 @@ public class FFmpegUtil {
*/
public static String[] extractAudio(String inputPath, String outputPath) {
//-vn: disable video
String mixAudioCmd = "ffmpeg -i %s -acodec copy -vn %s";
mixAudioCmd = String.format(mixAudioCmd, inputPath, outputPath);
return mixAudioCmd.split(" ");
String extractAudioCmd = "ffmpeg -i %s -acodec copy -vn %s";
extractAudioCmd = String.format(extractAudioCmd, inputPath, outputPath);
return extractAudioCmd.split(" ");
}
/**
@ -128,9 +128,9 @@ public class FFmpegUtil {
*/
public static String[] extractVideo(String inputPath, String outputPath) {
//-an: disable audio
String mixAudioCmd = "ffmpeg -i %s -vcodec copy -an %s";
mixAudioCmd = String.format(mixAudioCmd, inputPath, outputPath);
return mixAudioCmd.split(" ");
String extractVideoCmd = "ffmpeg -i %s -vcodec copy -an %s";
extractVideoCmd = String.format(extractVideoCmd, inputPath, outputPath);
return extractVideoCmd.split(" ");
}
/**
@ -360,9 +360,9 @@ public class FFmpegUtil {
* @return encode audio success or not
*/
public static String[] encodeAudio(String inputPath, String outputPath, int sampleRate, int channel) {
String combineVideo = "ffmpeg -f s16le -ar %d -ac %d -i %s %s";
combineVideo = String.format(Locale.getDefault(), combineVideo, sampleRate, channel, inputPath, outputPath);
return combineVideo.split(" ");
String encodeAudioCmd = "ffmpeg -f s16le -ar %d -ac %d -i %s %s";
encodeAudioCmd = String.format(Locale.getDefault(), encodeAudioCmd, sampleRate, channel, inputPath, outputPath);
return encodeAudioCmd.split(" ");
}
/**
@ -551,9 +551,15 @@ public class FFmpegUtil {
* @return command of inserting picture
*/
public static String[] insertPicIntoVideo(String inputPath, String picturePath, String outputPath) {
String buildIndex = "ffmpeg -i %s -i %s -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic %s";
buildIndex = String.format(buildIndex, inputPath, picturePath, outputPath);
return buildIndex.split(" ");
String insertPicCmd = "ffmpeg -i %s -i %s -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic %s";
insertPicCmd = String.format(insertPicCmd, inputPath, picturePath, outputPath);
return insertPicCmd.split(" ");
}
public static String[] addSubtitleIntoVideo(String inputPath, String subtitlePath, String outputPath) {
String subtitleCmd = "ffmpeg -i %s -vf subtitles=%s %s";
subtitleCmd = String.format(subtitleCmd, inputPath, subtitlePath, outputPath);
return subtitleCmd.split(" ");
}
/**

@ -85,6 +85,13 @@
android:layout_marginTop="4dp"
android:text="@string/video_multi"/>
<Button
android:id="@+id/btn_add_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="@string/video_subtitle"/>
<Button
android:id="@+id/btn_reverse_video"
android:layout_width="wrap_content"

@ -53,6 +53,7 @@
<string name="video_speed">Playing speed</string>
<string name="video_flv">FLV index</string>
<string name="video_thumbnail">Insert thumbnail</string>
<string name="video_subtitle">add subtitle</string>
<string name="swap">Swap</string>
<string name="start">Start</string>

@ -53,6 +53,7 @@
<string name="video_speed">播放倍率</string>
<string name="video_flv">FLV索引</string>
<string name="video_thumbnail">插入封面</string>
<string name="video_subtitle">添加字幕</string>
<string name="swap">切换</string>
<string name="start">开始</string>

Loading…
Cancel
Save