简体   繁体   English

如何使用 FFMPEG 将视频文件转换为音频文件?

[英]How to convert video file to audio file by using FFMPEG?

I'm making video to audio converter app but need help to convert video to audio using ffmpeg.我正在制作视频到音频转换器应用程序,但需要帮助使用 ffmpeg 将视频转换为音频。 I checked many answers and websites but their answers are not easy to understand and I'm confused with command that is used to convert.我检查了许多答案和网站,但他们的答案并不容易理解,而且我对用于转换的命令感到困惑。 What is the proper command to convert video to audio and is I'm executing the command in right way?将视频转换为音频的正确命令是什么,我是否以正确的方式执行命令?

public class VideoConvertActivity extends AppCompatActivity {

    private VideoView videoView;
    private Button convertButton;
    private String filePath;
    private FFmpeg ffmpeg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_convert);

        videoView = findViewById(R.id.videoviewID);
        convertButton = findViewById(R.id.convertbuttonID);

        ffmpeg = FFmpeg.getInstance(VideoConvertActivity.this);

// in variable video i'm getting the path of video path from previous activity through intent and that video will have to convert to audio file
        Intent extras = getIntent();
        final String video = extras.getStringExtra("video");

        videoView.setVideoPath(video);
        videoView.start();

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

                try {

                    File moviesDir = Environment.getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_MUSIC
                    );

                    String filePrefix = "convert_audio";
                    String fileExtn = ".mp3";
                    File dest = new File(moviesDir, filePrefix + fileExtn);

                    int fileNo = 0;
                    while (dest.exists()) {
                        fileNo++;
                        dest = new File(moviesDir, filePrefix + fileNo + fileExtn);
                    }
                    filePath = dest.getAbsolutePath();

                    // to execute "ffmpeg -version" command you just need to pass "-version"
                    String[] command = {"-y", "-i", video, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", filePath};
                    ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {

                        @Override
                        public void onStart() {
                            Toast.makeText(VideoConvertActivity.this,"Started",Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onProgress(String message) {
                            Toast.makeText(VideoConvertActivity.this,"Progress",Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onFailure(String message) {
                            Toast.makeText(VideoConvertActivity.this,"Failed",Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onSuccess(String message) {
                            Toast.makeText(VideoConvertActivity.this,"Succeed",Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onFinish() {
                            Toast.makeText(VideoConvertActivity.this,"Finished",Toast.LENGTH_LONG).show();
                        }
                    });
                } catch (FFmpegCommandAlreadyRunningException e) {
                    // Handle if FFmpeg is already running
                }

                 Toast.makeText(VideoConvertActivity.this,"Converted Successfully",Toast.LENGTH_SHORT).show();

            }
        });

    }

}

我在清单中授予了读取权限,但忘记授予写入权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Main is not a place u should place a while loop, it will cause problems.Since main is a loop in it self u should use thread to do something in background and not interrupt the flow of program. Main不是你应该放置while循环的地方,它会导致问题。由于main本身就是一个循环你应该使用线程在后台做一些事情而不是中断程序流程。 In this case if will do.在这种情况下,如果可以。

Here is an example of player so u can understand how it works: https://developer.android.com/guide/topics/media/exoplayer这是播放器的示例,因此您可以了解它的工作原理: https : //developer.android.com/guide/topics/media/exoplayer

If u don't solve it in a few hours ill see what I can do to help you.如果你在几个小时内没有解决它,看看我能做些什么来帮助你。

any convert commend you should place it in your command variable任何转换推荐你都应该把它放在你的command变量中

so instrad of this commend所以在这个推荐中

String[] command = {"-y", "-i", video, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", filePath};

use this one使用这个

ffmpeg -i video.mp4 -f mp3 -ab 192000 -vn music.mp3

or you can format your commend as you did for command to be an array :或者您可以像将命令设置为数组一样格式化您的推荐:

   String[] command = {"-y", "-i", video, "-f", "mp3", "-ab", "192000", "-vn", filePath};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM