将图像组合成音频文件,并以编程方式制作视频文件 [英] Combine an image into audio file and make a video file in android programmatically

查看:163
本文介绍了将图像组合成音频文件,并以编程方式制作视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这看起来很复杂,环境值得到的错误为空。

>

我研究了很多,但没有找到任何解决方案。



Android中有没有办法合并图像和一个音频文件来制作视频?

解决方案

我没有编译FFMPEG,只是我使用 javacv for android ,解决了我的问题,结合音频和图像,并将其作为视频文件。
步骤:
首先按照提供的步骤这里,然后将以下代码放入您的活动中,并确保更改输入文件和输出文件的路径。

  new AsyncTask< Void,Void,Void>(){
ProgressDialog dialog;
FFmpegFrameRecorder recorder;
protected void onPreExecute(){
dialog = new ProgressDialog(RecordActivity.this);
dialog.setMessage(Genrating video,Please wait .........);
dialog.setCancelable(false);
dialog.show();
};

@Override
protected Void doInBackground(Void ... arg0){

文件夹= Environment.getExternalStorageDirectory();
String path = folder.getAbsolutePath()+/ DCIM / Camera;
// ArrayList< String> path =(ArrayList< String>)getListOfFiles(path,jpg);
long millis = System.currentTimeMillis();

videoPath = path +/+test_sham _+ millis +3gp;

尝试{



//音频抓取器
FrameGrabber grabber2 =新的FFmpegFrameGrabber(folder.getAbsolutePath()+/ Samsung /音乐/ Over_the_horizo​​n.mp3\" );

// video grabber
FrameGrabber grabber1 = new FFmpegFrameGrabber(path +/ 20140527_133034.jpg);
grabber1.start();

grabber2.start();

recorder = new FFmpegFrameRecorder(path
+/+test_sham _+ millis +3gp,grabber1.getImageWidth(),grabber1.getImageHeight(),2);

//recorder.setVideoCodec(5);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
// recorder.setVideoCodec(avcodec.AV_CODEC_ID_MP4ALS);

recorder.setFormat(3gp);
// recorder.setFormat(mp4);
recorder.setFrameRate(frameRate);
recorder.setSampleRate(grabber2.getSampleRate());
recorder.setVideoBitrate(30);
startTime = System.currentTimeMillis();
recorder.start();

框架frame1,frame2 = null;

while((frame1 = grabber1.grabFrame())!= null ||

(frame2 = grabber2.grabFrame())!= null){

recorder.record(frame1);

recorder.record(frame2);

}

recorder.stop();

grabber1.stop();

grabber2.stop();




System.out.println(Total Time: - + recorder.getTimestamp());

} catch(Exception e){
e.printStackTrace();
}

返回null;
}

protected void onPostExecute(Void result){
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(videoPath),video / 3gp);
startActivity(intent);
Toast.makeText(RecordActivity.this,Done :::+ videoPath,Toast.LENGTH_SHORT)
.show();
};
} .execute();

对于引用: ref1 ref2


I read the tutorial of ffmpeg library to combine the image into audio file.

This is looking very complex and getting error with environment value is null.

I researched a lot but didn't found any solution.

Is there any way in Android to merge an image and an audio file to make a video?

解决方案

I didnt compiled FFMPEG, simply I used javacv for android and resolved my problem to combine audio and image and make it as video file. Steps: First Follow the steps provided here and then place the below code in your activity and make sure that to change the path of input files and output file.

 new AsyncTask<Void, Void, Void>() {
            ProgressDialog dialog;
            FFmpegFrameRecorder recorder;
            protected void onPreExecute() {
                dialog = new ProgressDialog(RecordActivity.this);
                dialog.setMessage("Genrating video, Please wait.........");
                dialog.setCancelable(false);
                dialog.show();
            };

            @Override
            protected Void doInBackground(Void... arg0) {

                File folder = Environment.getExternalStorageDirectory();
                String path = folder.getAbsolutePath() + "/DCIM/Camera";
               // ArrayList<String> paths = (ArrayList<String>) getListOfFiles(path, "jpg");
                long millis = System.currentTimeMillis();

                videoPath = path + "/" + "test_sham_"+millis+".3gp";

                try {



                //audio grabber
                FrameGrabber grabber2 = new FFmpegFrameGrabber(folder.getAbsolutePath()+"/Samsung/Music/Over_the_horizon.mp3"); 

                //video grabber
                FrameGrabber grabber1 = new FFmpegFrameGrabber(path+"/20140527_133034.jpg"); 
                    grabber1.start();

                    grabber2.start();

                  recorder = new FFmpegFrameRecorder(path
                          + "/" + "test_sham_"+millis+".3gp",  grabber1.getImageWidth(), grabber1.getImageHeight(),2);

                    //recorder.setVideoCodec(5);
                    recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
                     // recorder.setVideoCodec(avcodec.AV_CODEC_ID_MP4ALS);

                    recorder.setFormat("3gp");
                  //  recorder.setFormat("mp4");
                    recorder.setFrameRate(frameRate);
                    recorder.setSampleRate(grabber2.getSampleRate());
                    recorder.setVideoBitrate(30);
                    startTime = System.currentTimeMillis();
                    recorder.start();

                    Frame frame1, frame2 = null; 

                    while ((frame1 = grabber1.grabFrame()) != null || 

                          (frame2 = grabber2.grabFrame()) != null) { 

                        recorder.record(frame1); 

                        recorder.record(frame2); 

                    } 

                    recorder.stop(); 

                    grabber1.stop(); 

                    grabber2.stop(); 




                    System.out.println("Total Time:- " + recorder.getTimestamp());

                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;
            }

            protected void onPostExecute(Void result) {
                dialog.dismiss();
                Intent intent = new Intent(Intent.ACTION_VIEW); 
                intent.setDataAndType(Uri.parse(videoPath), "video/3gp");
                startActivity(intent);
                Toast.makeText(RecordActivity.this, "Done:::"+videoPath, Toast.LENGTH_SHORT)
                        .show();
            };
        }.execute();

For references:ref1, ref2

这篇关于将图像组合成音频文件,并以编程方式制作视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆