最近在看如何将图片和音频结合起来..发现简单且以我的水平能实现的方法就是:合成视频!
尝试了很多遍下载ffmpeg自己编译so库导入 都失败了😭 最后试了下加依赖可以成功:
在build.gradle depenencies下添加(sync后自动下载)
implementation 'com.arthenica:mobile-ffmpeg-full-gpl:4.4'
言归正传,想要完成图片+音频=视频,简单设计一下就是:(1.2.部分以后再补上)
1.选图
–A. take a photo with camera
–B. choose from photos
2.录音
–A. record with sound recorder
–B. choose from recordings
3.合成(code如下)
String command = String.format("-y -loop 1 -i %s -i %s -c:v libx264 -tune stillimage -c:a copy -filter:v \"scale=-2:720,setsar=1:1\" -shortest %s", imagePath, audioPath, outputPath);
progressDialog = ProgressDialog.show(MainActivity.this,"FFMPEG","Compositing video, please wait");
FFmpeg.executeAsync(command, new ExecuteCallback() {
@Override
public void apply(long executionId, int returnCode) {
if (returnCode == Config.RETURN_CODE_SUCCESS) {
progressDialog.dismiss();
Log.i("MainActivity", "compose success");
Toast.makeText(getApplicationContext(), "outputPath = "+outputPath, Toast.LENGTH_SHORT).show();
playVideo(Uri.parse(outputPath));
//todo
//update Gallery
} else {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "compose fail!!", Toast.LENGTH_SHORT).show();
}
}
});
重点就是这条:
其中:
–
-loop
1
means to loop the picture //
循环播放
–
-
i
indicates the input file //
输入文件
–
-
c:v
libx264
indicates that the video is encoded with H.264 //
用
H.264
编码
–
-tune
stillimage
indicates the encoding of optimized still images //
优化静态图像
–
-
c:a
copy
indicates the copied audio encoding format //
复制音频编码格式
–
-shortest
Indicates that the input stream with the shortest length is selected as the length of the output stream //
选择长度最短的输入流作为输出流的长度
–
-
filter:v
“scale=-2:720,setsar=1:1”
-2
: The width is guaranteed to be a multiple of 2
;
720
: Height ;
setsar
=1:1
: maintains the pixel aspect ratio
注:-2是考虑到libx264要求高度为偶数
增加一条命令:(将视频分离,替换音频后重新合成视频)
其中:
–
-
c:v
copy
indicates the encoding format of the copied video ///
复制视频编码格式
–
-map 0:v:0
indicates that the video stream of the first input file is selected //
选第一个输入文件的视频流
–
-map 1:a:0
indicates the selection of the audio stream for the second input file //
选第二个输入文件的音频流
大概就这些啦,生成视频要立马在相册可见,可以增加以下code:
//update Gallery
MediaScannerConnection mMediaScanner = new MediaScannerConnection(MainActivity.this, null);mMediaScanner.connect();if (mMediaScanner !=null && mMediaScanner.isConnected()) { mMediaScanner.scanFile(outputPath,null);}