How To Make Mp4 File From Images?
Solution 1:
Please check the below code
Make one file FfmpegController.java
publicclassFfmpegController {
privatestaticContext mContext;
privatestaticUtility mUtility;
privatestaticString mFfmpegBinaryPath;
publicFfmpegController(Context context) {
mContext = context;
mUtility = newUtility(context);
initFfmpeg();
}
privatevoidinitFfmpeg()
{
/*
Save the ffmpeg binary to app internal storage, so we can use it by executing java runtime command.
*/
mFfmpegBinaryPath = mContext.getApplicationContext().getFilesDir().getAbsolutePath() + "/ffmpeg";
if (Utility.isFileExsisted(mFfmpegBinaryPath))
return;
InputStream inputStream = mContext.getResources().openRawResource(R.raw.ffmpeg);
mUtility.saveFileToAppInternalStorage(inputStream, "ffmpeg");
Utility.excuteCommand(CommandHelper.commandChangeFilePermissionForExecuting(mFfmpegBinaryPath));
}
publicvoidconvertImageToVideo(String inputImgPath)
{
/*
Delete previous video.
*/Log.e("Image Parth", "inputImgPath - "+inputImgPath);
if (Utility.isFileExsisted(pathOuputVideo()))
Utility.deleteFileAtPath(pathOuputVideo());
/*
Save the command into a shell script.
*/saveShellCommandImg2VideoToAppDir(inputImgPath);
Utility.excuteCommand("sh" + " " + pathShellScriptImg2Video());
}
publicStringpathOuputVideo()
{
return mUtility.getPathOfAppInternalStorage() + "/out.mp4";
}
privateStringpathShellScriptImg2Video()
{
return mUtility.getPathOfAppInternalStorage() + "/img2video.sh";
}
privatevoidsaveShellCommandImg2VideoToAppDir(String inputImgPath)
{
String command = CommandHelper.commandConvertImgToVideo(mFfmpegBinaryPath, inputImgPath, pathOuputVideo());
InputStream is = newByteArrayInputStream(command.getBytes());
mUtility.saveFileToAppInternalStorage(is, "img2video.sh");
}
}
Make another Java file Utility.java
publicclassUtility {
privatefinalstaticStringTAG= Utility.class.getName();
privatestatic Context mContext;
publicUtility(Context context) {
mContext = context;
}
publicstatic String excuteCommand(String command)
{
try {
Log.d(TAG, "execute command : " + command);
Processprocess= Runtime.getRuntime().exec(command);
BufferedReaderreader=newBufferedReader(
newInputStreamReader(process.getInputStream()));
int read;
char[] buffer = newchar[4096];
StringBufferoutput=newStringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
process.waitFor();
Log.d(TAG, "command result: " + output.toString());
return output.toString();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
} catch (InterruptedException e) {
Log.e(TAG, e.getMessage(), e);
}
return"";
}
public String getPathOfAppInternalStorage()
{
return mContext.getApplicationContext().getFilesDir().getAbsolutePath();
}
publicvoidsaveFileToAppInternalStorage(InputStream inputStream, String fileName)
{
Filefile=newFile(getPathOfAppInternalStorage() + "/" + fileName);
if (file.exists())
{
Log.d(TAG, "SaveRawToAppDir Delete Exsisted File");
file.delete();
}
FileOutputStream outputStream;
try {
outputStream = mContext.openFileOutput(fileName, Context.MODE_PRIVATE);
byte[] buffer = newbyte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
publicstaticbooleanisFileExsisted(String filePath)
{
Filefile=newFile(filePath);
return file.exists();
}
publicstaticvoiddeleteFileAtPath(String filePath)
{
Filefile=newFile(filePath);
file.delete();
}
}
Make another file CommandHelper.java
publicclassCommandHelper {
publicstaticStringcommandConvertImgToVideo(String ffmpegBinaryPath, String inputImgPath, String outputVideoPath) {
Log.e("ffmpegBinaryPath", "ffmpegBinaryPath - "+ffmpegBinaryPath);
Log.e("inputImgPath", "inputImgPath - "+inputImgPath);
Log.e("outputVideoPath", "outputVideoPath - "+outputVideoPath);
return ffmpegBinaryPath + " -r 1/1 -i " + inputImgPath + " -c:v libx264 -crf 23 -pix_fmt yuv420p -s 640x480 " + outputVideoPath;
}
publicstaticStringcommandChangeFilePermissionForExecuting(String filePath) {
return"chmod 777 " + filePath;
}
}
When you want to execute code and make images to video please use below code.
AsyncTask asyncTask = newAsyncTask() {
ProgressDialog mProgressDialog;
@OverrideprotectedvoidonPreExecute() {
/* mProgressDialog = new ProgressDialog(activity.this);
mProgressDialog.setMessage("Converting...");
mProgressDialog.setCancelable(false);
mProgressDialog.show();*/Log.e("Video Process Start", "======================== Video Process Start ======================================");
}
@OverrideprotectedObjectdoInBackground(Object... params) {
saveInputImgToAppInternalStorage();
/* for(int i = 1; i<11 ; i++){
mFfmpegController.convertImageToVideo(mUtility.getPathOfAppInternalStorage() + "/" + "Img"+i+".jpg");
}
*/
mFfmpegController.convertImageToVideo(mUtility.getPathOfAppInternalStorage() + "/" + "img%05d.jpg");
returnnull;
}
@OverrideprotectedvoidonPostExecute(Object o) {
// mProgressDialog.dismiss();Log.e("Video Process Complete", "======================== Video Process Complete ======================================");
Log.e("Video Path", "Path - "+mFfmpegController.pathOuputVideo());
Toast.makeText(activity.this, "Video Process Complete", Toast.LENGTH_LONG).show();
stopSelfResult(lateststartid);
Common.ScreenshotCounter = 0;
Common.ScreenshotTimerCounter = 0;
/*try {
copyFile(new FileInputStream(mFfmpegController.pathOuputVideo()), new FileOutputStream(Common.strPathForVideos));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
};
Please note:
Captured images must be in format like:
Img00001, Img00002 .......
Because FFMPEG code expects it this way.
Fo those who didn't find R.raw.ffmpeg : Go To : https://github.com/guardianproject/android-ffmpeg-java/blob/master/res/raw/ffmpeg
Solution 2:
There's plenty of tools for editing videos like INDE, FFMPEG and etc.
INDE has so many functionality to join videos and images.
If you decide to use FFMPEG then this link provides steps for integration of this tool
Other helpful links:
FFMPEG:Multiple Image frames + 1 Audio =1 Video
Android make animated video from list of images
Android ffmpeg: create video from sequnce of images using jni
Combine an image into audio file and make a video file in android programmatically
Video creation from series of images?
How to Build Android Applications Based on FFmpeg by An Example
Solution 3:
you can try this libray
https://github.com/sannies/mp4parser
in this library they mention that you create movie from images(Jpeg image).
Post a Comment for "How To Make Mp4 File From Images?"