当前位置: 首页 > 知识库问答 >
问题:

如何从图像制作MP4文件?[关闭]

尤茂材
2023-03-14

谁能告诉我如何在android中组合图像并生成一个mp4文件,并将视频文件存储在SDCard中?

共有1个答案

穆铭晨
2023-03-14

请检查下面的代码

创建一个文件FFMPEGController.java

public class FfmpegController {

    private static Context mContext;
    private static Utility mUtility;
    private static String mFfmpegBinaryPath;

    public FfmpegController(Context context) {

        mContext = context;

        mUtility = new Utility(context);

        initFfmpeg();
    }

    private void initFfmpeg()
    {
        /*
        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));
    }

    public void convertImageToVideo(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());
    }

    public String pathOuputVideo()
    {
        return mUtility.getPathOfAppInternalStorage() + "/out.mp4";
    }

    private String pathShellScriptImg2Video()
    {
        return mUtility.getPathOfAppInternalStorage() + "/img2video.sh";
    }

    private void saveShellCommandImg2VideoToAppDir(String inputImgPath)
    {
        String command = CommandHelper.commandConvertImgToVideo(mFfmpegBinaryPath, inputImgPath, pathOuputVideo());

        InputStream is = new ByteArrayInputStream(command.getBytes());

        mUtility.saveFileToAppInternalStorage(is, "img2video.sh");
    }
}

制作另一个Java文件Utility.Java

public class Utility {

    private final static String TAG = Utility.class.getName();
    private static Context mContext;

    public Utility(Context context) {
        mContext = context;
    }

    public static String excuteCommand(String command)
    {
        try {
            Log.d(TAG, "execute command : " + command);

            Process process = Runtime.getRuntime().exec(command);

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            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();
    }

    public void saveFileToAppInternalStorage(InputStream inputStream, String fileName)
    {
        File file = new File(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 = new byte[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);
        }
    }

    public static boolean isFileExsisted(String filePath)
    {
        File file = new File(filePath);
        return file.exists();
    }

    public static void deleteFileAtPath(String filePath)
    {
        File file = new File(filePath);
        file.delete();
    }
}
public class CommandHelper {
    public static String commandConvertImgToVideo(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;
    }

    public static String commandChangeFilePermissionForExecuting(String filePath) {
        return "chmod 777 " + filePath;
    }
}
AsyncTask asyncTask = new AsyncTask() {

         ProgressDialog mProgressDialog;

         @Override
         protected void onPreExecute() {
            /* mProgressDialog = new ProgressDialog(activity.this);

             mProgressDialog.setMessage("Converting...");

             mProgressDialog.setCancelable(false);

             mProgressDialog.show();*/

             Log.e("Video Process Start", "======================== Video Process Start ======================================");
         }

         @Override
         protected Object doInBackground(Object... params) {

            saveInputImgToAppInternalStorage();
/*           for(int i = 1; i<11 ; i++){
             mFfmpegController.convertImageToVideo(mUtility.getPathOfAppInternalStorage() + "/" + "Img"+i+".jpg");
             }
*/

            mFfmpegController.convertImageToVideo(mUtility.getPathOfAppInternalStorage() + "/" + "img%05d.jpg");
             return null;
         }

         @Override
         protected void onPostExecute(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();
            }*/

         }
     };

那些没有找到r.raw.ffmpeg的人:请转到:https://github.com/guardianproject/android-ffmpeg-java/blob/master/res/raw/ffmpeg

 类似资料:
  • 我正在玩gstream er命令行界面,在从PNG图像创建mp4视频文件时卡住了。你们能帮忙解决这个问题吗? 使用下面的命令,我从摄像机创建了PNG图像: gst-launch-1.0。exe-v ksvideosrc!队列decodebin!视频转换!pngenc!多文件接收器位置=“frame  d.png” 我可以使用以下命令播放pPNG图像: gst-launch-1.0-v multi

  • 我用ffmpeg库为android转换了一个图像到mp4视频,但这需要太长时间,我想避免第三方库,如果可能的话。请帮我解决我的问题。 谢谢你。

  • 问题内容: 我需要从服务器上的PDF文件中提取所有图像。我不想要PDF页面,只想要原始尺寸和分辨率的图像。 如何使用Perl,PHP或任何其他基于UNIX的应用程序(我将使用PHP的exec函数调用它)来做到这一点? 问题答案: pdfimages就是这样做的。它是poppler- utils和xpdf-utils软件包的一部分。 从联机帮助页: Pdfimages将可移植文档格式(PDF)文件中

  • 我已经看到了使用workbook.getAllPictures()从Excel工作簿中提取图像文件并将其保存到本地驱动器的示例代码。但是,我只需要从某些工作表中提取图像文件。有人能提供如何实现这一点的示例代码吗?

  • 读取图像文件信息 使用图像读取器读取文件头中的信息: #include <stdio.h> #include <LCUI/LCUI.h> #include <LCUI/image.h> ​ int main(int argc, char *argv[]) { FILE *fp; LCUI_ImageReaderRec reader = { 0 }; if (ar

  • 我正在使用PrimeFaces的图表组件。我想从该组件获取图像(如png)。 我怎么做? 我尝试了html2画布和画布,但我不能得到相同的图像。我也尝试https://github.com/jsplumb/jsPlumb/issues/35#issuecomment-82196174,但不好。 我的代码如下。 DiagramShowView。JAVA Diagram.xhtml 下面是浏览器输出。