我正在制作视频到音频转换器应用程序,但需要帮助才能使用ffmpeg将视频转换为音频。我检查了许多答案和网站,但它们的答案不容易理解,我对用于转换的命令感到困惑。将视频转换为音频的正确命令是什么,我是否以正确的方式执行该命令?
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();
}
});
}
}
答案 0 :(得分:0)
Main不是一个地方,您不应该放置while循环,否则会引起问题。由于main是一个循环,因此您应该使用线程在后台执行某些操作,而不中断程序的流程。 在这种情况下,如果可以。
以下是玩家的示例,因此您可以了解其工作方式: https://developer.android.com/guide/topics/media/exoplayer
如果您几个小时后仍无法解决,请看看我能为您做些什么。
答案 1 :(得分:0)
任何建议您将其放入command
变量
如此赞美
String[] command = {"-y", "-i", video, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", filePath};
使用这个
ffmpeg -i video.mp4 -f mp3 -ab 192000 -vn music.mp3
或者您也可以像将命令设置为数组一样格式化命令:
String[] command = {"-y", "-i", video, "-f", "mp3", "-ab", "192000", "-vn", filePath};
答案 2 :(得分:0)
我在清单中授予了读取权限,但忘记了给予写入权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>