Ffmpeg- Split video into 30 seconds parts in android

旧时模样 提交于 2019-12-02 22:05:25

问题


I am working on a project. In which I want to split the video into 30 sec equal parts. If the video is 45 sec long than ideally in 30 and 15-sec parts. I already created a method which takes the starting and the ending point and run the Ffmpeg command to cut the video. This is what I created so far

   private void executeCutVideoCommand(int startMs, int endMs) {
        File moviesDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MOVIES
        );

        String filePrefix = "cut_video";
        String fileExtn = ".mp4";
        String yourRealPath = getPath(VideoCutterActivity.this, selectedVideoUri);
        File dest = new File(moviesDir, filePrefix + fileExtn);
        int fileNo = 0;
        while (dest.exists()) {
            fileNo++;
            dest = new File(moviesDir, filePrefix + fileNo + fileExtn);
        }

        filePath = dest.getAbsolutePath();
        String[] complexCommand = {"-ss", "" + startMs / 1000, "-y", "-i", yourRealPath, "-t", "" + (endMs - startMs) / 1000,"-vcodec", "mpeg4", "-b:v", "2097152", "-b:a", "48000", "-ac", "2", "-ar", "22050", filePath};

        execFFmpegBinary(complexCommand);
        MediaScannerConnection.scanFile(VideoCutterActivity.this,
                new String[] { filePath },
                null,
                new MediaScannerConnection.OnScanCompletedListener() {

                    public void onScanCompleted(String path, Uri uri) {

                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });


    }

    /**
     * Executing ffmpeg binary
     */
    private void execFFmpegBinary(final String[] command) {
        try {
            ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
                @Override
                public void onFailure(String s) {
                    Log.d(TAG, "FAILED with output : " + s);
                }

                @Override
                public void onSuccess(String s) {
                    Log.d(TAG, "SUCCESS with output : " + s);
                        Intent intent = new Intent(VideoCutterActivity.this, PreviewActivity.class);
                        intent.putExtra(FILEPATH, filePath);
                        startActivity(intent);
                }

                @Override
                public void onProgress(String s) {
                    progressDialog.setMessage("progress : " + s);
                }

                @Override
                public void onStart() {
                    progressDialog.setMessage("Processing...");
                    progressDialog.show();
                }

                @Override
                public void onFinish() {
                    progressDialog.dismiss();


                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            // do nothing for now
        }
    }

i tried to add a loop in method Calling But than app crashes

    cutVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                choice = 2;

                if (selectedVideoUri != null) {
                    int start,end;
                    for (int i=0;i<=duration;i=i+30){
                        start=i;
                        end=start+30;
                        executeCutVideoCommand(start,end);

                    }
//                    executeCutVideoCommand(rangeSeekBar.getSelectedMinValue().intValue() * 1000, rangeSeekBar.getSelectedMaxValue().intValue() * 1000);

                } else
                    Snackbar.make(mainlayout, "Please upload a video", 4000).show();
            }
        });


    }

I hope now you can what i want to do. I want to Ffmpeg command or method which split the video into 30-sec parts and place them on the given filepath. Let me know if we you wanna know more about the code. Help is much appreciated

来源:https://stackoverflow.com/questions/58998183/ffmpeg-split-video-into-30-seconds-parts-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!