VideoConUtil.java 4.96 KB
package com.sw.laryngoscope.imageToVideo;


import android.graphics.Bitmap;
import android.view.View;

import androidx.annotation.Nullable;

import com.mobile.ffmpeg.util.FFmpegAsyncUtils;
import com.mobile.ffmpeg.util.FFmpegExecuteCallback;
import com.sw.laryngoscope.utils.Logger;
import com.sw.laryngoscope.utils.ShellUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class VideoConUtil {
    public String TAG = "VideoConUtil";

    public void createVideo(String path, String sourcepath, Bitmap bitmap, int width, int height, int rate) {
        ImageToVideoUtil2 imageToVideoUtil2 = new ImageToVideoUtil2();
        imageToVideoUtil2.init(width, height);
        for(int i = 0; i < rate * 10 * 1000 / 1000; i++) {
            imageToVideoUtil2.drawframe(bitmap, i);
            if ( mVideoCallback != null ) {
                mVideoCallback.onVideoProgress( Math.round(i * 100 / ( rate *10.0f )) );
            }
        }
        imageToVideoUtil2.drainEnd();

        String listPath = "/sdcard/videolist.txt";
        BufferedWriter writer = null;
        try {
            if ( !(new File(listPath)).exists() ) {
                (new File(listPath)).createNewFile();
            }
            writer = new BufferedWriter(new FileWriter(listPath));
            writer.write("file '/sdcard/outputbitmap.mp4'\n");
            writer.write("file '"+ sourcepath + "'");
            writer.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        /*ShellUtils.execCommand("echo " + "file \'/sdcard/outputbitmap.mp4\'\nfile \'"
                + sourcepath + "\'" + " > /sdcard/videolist.txt", true);*/

        String[] cmd = new String[9];
        cmd[0] ="-f";
        cmd[1] ="concat";
        cmd[2] ="-safe";
        cmd[3] ="0";
        cmd[4] = "-i";
        cmd[5] = "/sdcard/videolist.txt";
        cmd[6] = "-c";
        cmd[7] = "copy";
        cmd[8] = path;

        FFmpegAsyncUtils asyncTask = new FFmpegAsyncUtils();
        asyncTask.setCallback(new FFmpegExecuteCallback() {
            @Override
            public void onFFmpegStart() {}
            @Override
            public void onFFmpegSucceed(@Nullable String executeOutput) {
                Logger.d(TAG, "=====concact====2 ");
                if ( mVideoCallback != null ) {
                    mVideoCallback.onVideoSuc("");
                }
            }
            @Override
            public void onFFmpegFailed(@Nullable String executeOutput) {
                Logger.d(TAG, "=====concact====3 ");
                if ( mVideoCallback != null ) {
                    mVideoCallback.onVideoFail();
                }
            }
            @Override
            public void onFFmpegProgress(@Nullable Integer progress) {
                Logger.d(TAG, "=====progress====3 ");
                if ( mVideoCallback != null ) {
                    mVideoCallback.onVideoProgress( 100 );
                }
            }
            @Override
            public void onFFmpegCancel() {
                Logger.d(TAG, "=====concact====03 ");
                if ( mVideoCallback != null ) {
                    mVideoCallback.onVideoFail();
                }
            }
        });
        asyncTask.execute(cmd);
    }

    public Bitmap saveViewBitmap(View view, int dstWidth, int dstHeight){
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = null;
        try{
            if ( null != view.getDrawingCache() ) {
                bitmap = Bitmap.createScaledBitmap(
                        view.getDrawingCache(), dstWidth, dstHeight, false );
                Logger.d("saveViewBitmap ");
            }
        } catch ( OutOfMemoryError e ) {
            e.printStackTrace();
            Logger.d("saveViewBitmap " + e.toString());
        } finally {
            view.setDrawingCacheEnabled(false);
            view.destroyDrawingCache();
        }

        return bitmap;
    }

    public void saveImage(Bitmap bmp, String path, String name) {
        FileOutputStream out = null;
        try {
            File folder = new File(path);
            if ( !folder.exists() ) {
                folder.mkdirs();
            }
            File file = new File(folder, name);
            out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
            Logger.d("saveImage ");
        } catch (Exception e) {
            Logger.d("saveImage " + e.toString());
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
            }
        }
    }

    public VideoCallback mVideoCallback;

    public void setVideoCallback(VideoCallback pdfCallback) {
        this.mVideoCallback = pdfCallback;
    }

    public interface VideoCallback {
        void onVideoSuc(String path);
        void onVideoFail();
        void onVideoProgress(int pro);
    }
 
}