浅用FFmpeg命令

最近在看如何将图片和音频结合起来..发现简单且以我的水平能实现的方法就是:合成视频!

尝试了很多遍下载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);}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值