如何通过捕获android或java中的动画视图制作视频文件?

Hos*_*ifi 10 java android video-capture

我已经imageView从屏幕的一侧到另一侧制作了一个动画.这是java代码:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView imageView = findViewById(R.id.imageView);
        Button button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleAnimation(imageView);
            }
        });
    }
    public void handleAnimation(View view) {
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "x", 1000f);
        animatorX.setDuration(2000);
        animatorX.start();
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我们在用户点击ANIMATE按钮时看到的内容: 在此输入图像描述

现在我的问题是如何通过捕获动画制作视频文件imageView

编辑:

我需要的是:我想创建一个应用程序,它从用户那里拍摄一些照片,并对照片和一些效果制作一些动画,并将它们与所需的声音混合,最后导出视频剪辑.当然,如果我能,我宁愿把所有这些东西都隐藏起来.

Niz*_*ale 5

您必须录制屏幕,然后使用视图的xy坐标裁剪视频。您可以使用MediaProject APIandroid (5) 及更高版本录制屏幕。

 private VirtualDisplay mVirtualDisplay;
    private MediaRecorder mMediaRecorder;
    private MediaProjection mMediaProjection;
    private MediaProjectionCallback callback;

 MediaProjectionManager projectionManager = (MediaProjectionManager) 
        context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        mMediaProjection.registerCallback(callback, null);
        initRecorder();
        mMediaRecorder.prepare();
        mVirtualDisplay = createVirtualDisplay();
        mMediaRecorder.start();


public void initRecorder() {
        path = "/sdcard/Record/video" + ".mp4";
        recId = "capture-" + System.currentTimeMillis() + ".mp4";
        File myDirectory = new File(Environment.getExternalStorageDirectory(), "Record");

            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mMediaRecorder.setVideoEncodingBitRate(MainFragment.bitRate);          
            mMediaRecorder.setVideoFrameRate(30);
            mMediaRecorder.setVideoSize(MainFragment.DISPLAY_WIDTH,
            MainFragment.DISPLAY_HEIGHT);
            mMediaRecorder.setOutputFile(path);
    }


    private VirtualDisplay createVirtualDisplay() {
        return mMediaProjection.createVirtualDisplay("MainActivity",
                MainFragment.DISPLAY_WIDTH, MainFragment.DISPLAY_HEIGHT, MainFragment.screenDensity,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
    }

public class MediaProjectionCallback extends MediaProjection.Callback {
        @Override
        public void onStop() {
            mMediaRecorder.stop();
            // mMediaRecorder.reset();
            mMediaRecorder.release();
            mMediaProjection.unregisterCallback(callback);
            mMediaProjection = null;
            mMediaRecorder = null;
        }
Run Code Online (Sandbox Code Playgroud)

完成后,只需调用mMediaProjection.stop()以完成录制并将视频另存为tmp 之后,您就可以xy使用FFmpeg在视图所在的坐标处裁剪视频

ffmpeg -i in.mp4 -filter:v "crop=out_w:out_h:x:y" out.mp4
Run Code Online (Sandbox Code Playgroud)

其中选项如下:

out_w 是输出矩形的宽度

out_h 是输出矩形的高度

xy指定输出矩形的左上角

所以在你的情况下

String cmd ="-i '"+ tmpVideoPath+"' -filter:v "+"'crop="+view.getWidth()+":"+view.getHeight()+":"+view.getX()+":"+view.getY()+"'"+" -c:a copy "+outVideoPath
FFmpeg ffmpeg = FFmpeg.getInstance(context);
  // to execute "ffmpeg -version" command you just need to pass "-version"
ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

    @Override
    public void onStart() {}

    @Override
    public void onProgress(String message) {}

    @Override
    public void onFailure(String message) {}

    @Override
    public void onSuccess(String message) {}

    @Override
    public void onFinish() {}

});
Run Code Online (Sandbox Code Playgroud)


Mar*_*ain 0

有两种可能的方法可以对此进行存档。

1-您可以通过使用javacv 库 (FFmpeg)组合从视图中获取的一组位图来 实现此目的

FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("/sdcard/test.mp4",256,256);
try {
    recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
    recorder.setFormat("mp4");
    recorder.setFrameRate(30);
    recorder.setPixelFormat(avutil.PIX_FMT_YUV420P10);
    recorder.setVideoBitrate(1200);
    recorder.startUnsafe();
    for (int i=0;i< 5;i++)
    {
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        recorder.record(bitmap);
    }
    recorder.stop();
}
catch (Exception e){
    e.printStackTrace();
}  
Run Code Online (Sandbox Code Playgroud)

使用该库的所有代码都在这里

2-您可以使用此链接录制屏幕并根据您的需要使用。
屏幕录像机