|
|
@ -22,6 +22,10 @@ import java.io.File; |
|
|
|
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_BEGIN; |
|
|
|
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_BEGIN; |
|
|
|
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH; |
|
|
|
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* video process by FFmpeg command |
|
|
|
|
|
|
|
* Created by frank on 2018/1/25. |
|
|
|
|
|
|
|
*/ |
|
|
|
public class VideoHandleActivity extends BaseActivity { |
|
|
|
public class VideoHandleActivity extends BaseActivity { |
|
|
|
|
|
|
|
|
|
|
|
private final static String TAG = VideoHandleActivity.class.getSimpleName(); |
|
|
|
private final static String TAG = VideoHandleActivity.class.getSimpleName(); |
|
|
@ -104,7 +108,7 @@ public class VideoHandleActivity extends BaseActivity { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 调用ffmpeg处理视频 |
|
|
|
* call FFmpeg cmd to process video |
|
|
|
* |
|
|
|
* |
|
|
|
* @param srcFile srcFile |
|
|
|
* @param srcFile srcFile |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -118,11 +122,11 @@ public class VideoHandleActivity extends BaseActivity { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
switch (viewId) { |
|
|
|
switch (viewId) { |
|
|
|
case R.id.btn_video_transform://视频转码:mp4转flv、wmv, 或者flv、wmv转Mp4
|
|
|
|
case R.id.btn_video_transform://transform format
|
|
|
|
String transformVideo = PATH + File.separator + "transformVideo.mp4"; |
|
|
|
String transformVideo = PATH + File.separator + "transformVideo.mp4"; |
|
|
|
commandLine = FFmpegUtil.transformVideo(srcFile, transformVideo); |
|
|
|
commandLine = FFmpegUtil.transformVideo(srcFile, transformVideo); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_video_cut://视频剪切
|
|
|
|
case R.id.btn_video_cut://cut video
|
|
|
|
String suffix = FileUtil.getFileSuffix(srcFile); |
|
|
|
String suffix = FileUtil.getFileSuffix(srcFile); |
|
|
|
if (suffix == null || suffix.isEmpty()) { |
|
|
|
if (suffix == null || suffix.isEmpty()) { |
|
|
|
return; |
|
|
|
return; |
|
|
@ -132,7 +136,7 @@ public class VideoHandleActivity extends BaseActivity { |
|
|
|
int duration = 20; |
|
|
|
int duration = 20; |
|
|
|
commandLine = FFmpegUtil.cutVideo(srcFile, startTime, duration, cutVideo); |
|
|
|
commandLine = FFmpegUtil.cutVideo(srcFile, startTime, duration, cutVideo); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_video_concat://视频合并
|
|
|
|
case R.id.btn_video_concat://concat video
|
|
|
|
// commandLine = FFmpegUtil.toTs(srcFile, ts1);
|
|
|
|
// commandLine = FFmpegUtil.toTs(srcFile, ts1);
|
|
|
|
// concatStep ++;
|
|
|
|
// concatStep ++;
|
|
|
|
// String concatVideo = PATH + File.separator + "concatVideo.mp4";
|
|
|
|
// String concatVideo = PATH + File.separator + "concatVideo.mp4";
|
|
|
@ -150,19 +154,19 @@ public class VideoHandleActivity extends BaseActivity { |
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// commandLine = FFmpegUtil.concatVideo(srcFile, concatFile.getAbsolutePath(), concatVideo);
|
|
|
|
// commandLine = FFmpegUtil.concatVideo(srcFile, concatFile.getAbsolutePath(), concatVideo);
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_screen_shot://视频截图
|
|
|
|
case R.id.btn_screen_shot://video snapshot
|
|
|
|
String screenShot = PATH + File.separator + "screenShot.jpg"; |
|
|
|
String screenShot = PATH + File.separator + "screenShot.jpg"; |
|
|
|
int time = 18; |
|
|
|
int time = 18; |
|
|
|
commandLine = FFmpegUtil.screenShot(srcFile, time, screenShot); |
|
|
|
commandLine = FFmpegUtil.screenShot(srcFile, time, screenShot); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_water_mark://视频添加水印
|
|
|
|
case R.id.btn_water_mark://add watermark to video
|
|
|
|
//1、图片
|
|
|
|
// picture
|
|
|
|
String photo = PATH + File.separator + "launcher.png"; |
|
|
|
String photo = PATH + File.separator + "launcher.png"; |
|
|
|
String photoMark = PATH + File.separator + "photoMark.mp4"; |
|
|
|
String photoMark = PATH + File.separator + "photoMark.mp4"; |
|
|
|
String mResolution = "720x1280"; |
|
|
|
String mResolution = "720x1280"; |
|
|
|
int bitRate = 1024; |
|
|
|
int bitRate = 1024; |
|
|
|
commandLine = FFmpegUtil.addWaterMark(srcFile, photo, mResolution, bitRate, photoMark); |
|
|
|
commandLine = FFmpegUtil.addWaterMark(srcFile, photo, mResolution, bitRate, photoMark); |
|
|
|
//2、文字
|
|
|
|
// text
|
|
|
|
// String text = "Hello,FFmpeg";
|
|
|
|
// String text = "Hello,FFmpeg";
|
|
|
|
// String textPath = PATH + File.separator + "text.jpg";
|
|
|
|
// String textPath = PATH + File.separator + "text.jpg";
|
|
|
|
// boolean result = BitmapUtil.textToPicture(textPath, text, this);
|
|
|
|
// boolean result = BitmapUtil.textToPicture(textPath, text, this);
|
|
|
@ -170,7 +174,7 @@ public class VideoHandleActivity extends BaseActivity { |
|
|
|
// String textMark = PATH + File.separator + "textMark.mp4";
|
|
|
|
// String textMark = PATH + File.separator + "textMark.mp4";
|
|
|
|
// commandLine = FFmpegUtil.addWaterMark(srcFile, textPath, textMark);
|
|
|
|
// commandLine = FFmpegUtil.addWaterMark(srcFile, textPath, textMark);
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_generate_gif://视频转成gif
|
|
|
|
case R.id.btn_generate_gif://convert video into gif
|
|
|
|
String Video2Gif = PATH + File.separator + "Video2Gif.gif"; |
|
|
|
String Video2Gif = PATH + File.separator + "Video2Gif.gif"; |
|
|
|
int gifStart = 30; |
|
|
|
int gifStart = 30; |
|
|
|
int gifDuration = 5; |
|
|
|
int gifDuration = 5; |
|
|
@ -179,13 +183,13 @@ public class VideoHandleActivity extends BaseActivity { |
|
|
|
commandLine = FFmpegUtil.generateGif(srcFile, gifStart, gifDuration, |
|
|
|
commandLine = FFmpegUtil.generateGif(srcFile, gifStart, gifDuration, |
|
|
|
resolution, frameRate, Video2Gif); |
|
|
|
resolution, frameRate, Video2Gif); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_screen_record://屏幕录制
|
|
|
|
case R.id.btn_screen_record://screen recording
|
|
|
|
// String screenRecord = PATH + File.separator + "screenRecord.mp4";
|
|
|
|
// String screenRecord = PATH + File.separator + "screenRecord.mp4";
|
|
|
|
// String screenSize = "320x240";
|
|
|
|
// String screenSize = "320x240";
|
|
|
|
// int recordTime = 10;
|
|
|
|
// int recordTime = 10;
|
|
|
|
// commandLine = FFmpegUtil.screenRecord(screenSize, recordTime, screenRecord);
|
|
|
|
// commandLine = FFmpegUtil.screenRecord(screenSize, recordTime, screenRecord);
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_multi_video://视频画面拼接:分辨率、时长、封装格式不一致时,先把视频源转为一致
|
|
|
|
case R.id.btn_multi_video://combine video which layout could be horizontal of vertical
|
|
|
|
String input1 = PATH + File.separator + "input1.mp4"; |
|
|
|
String input1 = PATH + File.separator + "input1.mp4"; |
|
|
|
String input2 = PATH + File.separator + "input2.mp4"; |
|
|
|
String input2 = PATH + File.separator + "input2.mp4"; |
|
|
|
String outputFile = PATH + File.separator + "multi.mp4"; |
|
|
|
String outputFile = PATH + File.separator + "multi.mp4"; |
|
|
@ -194,16 +198,16 @@ public class VideoHandleActivity extends BaseActivity { |
|
|
|
} |
|
|
|
} |
|
|
|
commandLine = FFmpegUtil.multiVideo(input1, input2, outputFile, VideoLayout.LAYOUT_HORIZONTAL); |
|
|
|
commandLine = FFmpegUtil.multiVideo(input1, input2, outputFile, VideoLayout.LAYOUT_HORIZONTAL); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_reverse_video://视频反序倒播
|
|
|
|
case R.id.btn_reverse_video://video reverse
|
|
|
|
String output = PATH + File.separator + "reverse.mp4"; |
|
|
|
String output = PATH + File.separator + "reverse.mp4"; |
|
|
|
commandLine = FFmpegUtil.reverseVideo(srcFile, output); |
|
|
|
commandLine = FFmpegUtil.reverseVideo(srcFile, output); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_denoise_video://视频降噪
|
|
|
|
case R.id.btn_denoise_video://noise reduction of video
|
|
|
|
String denoise = PATH + File.separator + "denoise.mp4"; |
|
|
|
String denoise = PATH + File.separator + "denoise.mp4"; |
|
|
|
commandLine = FFmpegUtil.denoiseVideo(srcFile, denoise); |
|
|
|
commandLine = FFmpegUtil.denoiseVideo(srcFile, denoise); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_to_image://视频转图片
|
|
|
|
case R.id.btn_to_image://convert video to picture
|
|
|
|
String imagePath = PATH + File.separator + "Video2Image/";//图片保存路径
|
|
|
|
String imagePath = PATH + File.separator + "Video2Image/"; |
|
|
|
File imageFile = new File(imagePath); |
|
|
|
File imageFile = new File(imagePath); |
|
|
|
if (!imageFile.exists()) { |
|
|
|
if (!imageFile.exists()) { |
|
|
|
boolean result = imageFile.mkdir(); |
|
|
|
boolean result = imageFile.mkdir(); |
|
|
@ -211,25 +215,25 @@ public class VideoHandleActivity extends BaseActivity { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
int mStartTime = 10;//开始时间
|
|
|
|
int mStartTime = 10;//start time
|
|
|
|
int mDuration = 20;//持续时间(注意开始时间+持续时间之和不能大于视频总时长)
|
|
|
|
int mDuration = 20;//duration
|
|
|
|
int mFrameRate = 10;//帧率(从视频中每秒抽多少帧)
|
|
|
|
int mFrameRate = 10;//frameRate
|
|
|
|
commandLine = FFmpegUtil.videoToImage(srcFile, mStartTime, mDuration, mFrameRate, imagePath); |
|
|
|
commandLine = FFmpegUtil.videoToImage(srcFile, mStartTime, mDuration, mFrameRate, imagePath); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_pip://两个视频合成画中画
|
|
|
|
case R.id.btn_pip://combine into picture-in-picture video
|
|
|
|
String inputFile1 = PATH + File.separator + "beyond.mp4"; |
|
|
|
String inputFile1 = PATH + File.separator + "beyond.mp4"; |
|
|
|
String inputFile2 = PATH + File.separator + "small_girl.mp4"; |
|
|
|
String inputFile2 = PATH + File.separator + "small_girl.mp4"; |
|
|
|
if (!FileUtil.checkFileExist(inputFile1) && !FileUtil.checkFileExist(inputFile2)) { |
|
|
|
if (!FileUtil.checkFileExist(inputFile1) && !FileUtil.checkFileExist(inputFile2)) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
//x、y坐标点需要根据全屏视频与小视频大小,进行计算
|
|
|
|
//x and y coordinate points need to be calculated according to the size of full video and small video
|
|
|
|
//比如:全屏视频为320x240,小视频为120x90,那么x=200 y=150
|
|
|
|
//For example: full video is 320x240, small video is 120x90, so x=200 y=150
|
|
|
|
int x = 200; |
|
|
|
int x = 200; |
|
|
|
int y = 150; |
|
|
|
int y = 150; |
|
|
|
String picInPic = PATH + File.separator + "PicInPic.mp4"; |
|
|
|
String picInPic = PATH + File.separator + "PicInPic.mp4"; |
|
|
|
commandLine = FFmpegUtil.picInPicVideo(inputFile1, inputFile2, x, y, picInPic); |
|
|
|
commandLine = FFmpegUtil.picInPicVideo(inputFile1, inputFile2, x, y, picInPic); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case R.id.btn_moov://moov前移操作,针对mp4视频moov在mdat后面的情况
|
|
|
|
case R.id.btn_moov://moov box moves ahead, which is behind mdat box of mp4 video
|
|
|
|
if (!srcFile.endsWith(FileUtil.TYPE_MP4)) { |
|
|
|
if (!srcFile.endsWith(FileUtil.TYPE_MP4)) { |
|
|
|
showToast(getString(R.string.tip_not_mp4_video)); |
|
|
|
showToast(getString(R.string.tip_not_mp4_video)); |
|
|
|
return; |
|
|
|
return; |
|
|
@ -258,17 +262,17 @@ public class VideoHandleActivity extends BaseActivity { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 图片合成视频 |
|
|
|
* Combine pictures into video |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void handlePhoto() { |
|
|
|
private void handlePhoto() { |
|
|
|
// 图片所在路径,图片命名格式img+number.jpg
|
|
|
|
// The path of pictures, naming format: img+number.jpg
|
|
|
|
// 这里指定目录为根目录下img文件夹
|
|
|
|
// Here assigns the directory is img under the root path
|
|
|
|
String picturePath = PATH + "/img/"; |
|
|
|
String picturePath = PATH + "/img/"; |
|
|
|
if (!FileUtil.checkFileExist(picturePath)) { |
|
|
|
if (!FileUtil.checkFileExist(picturePath)) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
String combineVideo = PATH + File.separator + "combineVideo.mp4"; |
|
|
|
String combineVideo = PATH + File.separator + "combineVideo.mp4"; |
|
|
|
int frameRate = 2;// 合成视频帧率建议:1-10 普通视频帧率一般为25
|
|
|
|
int frameRate = 2;// suggested synthetic frameRate:1-10
|
|
|
|
String[] commandLine = FFmpegUtil.pictureToVideo(picturePath, frameRate, combineVideo); |
|
|
|
String[] commandLine = FFmpegUtil.pictureToVideo(picturePath, frameRate, combineVideo); |
|
|
|
if (ffmpegHandler != null) { |
|
|
|
if (ffmpegHandler != null) { |
|
|
|
ffmpegHandler.executeFFmpegCmd(commandLine); |
|
|
|
ffmpegHandler.executeFFmpegCmd(commandLine); |
|
|
|