Android 我可以使用MediaPlayer从音频文件自动创建字幕吗?
下面是我的代码 我可以通过编程方式创建mp3到.srt文件吗Android 我可以使用MediaPlayer从音频文件自动创建字幕吗?,android,android-studio,audio,android-mediaplayer,media-player,Android,Android Studio,Audio,Android Mediaplayer,Media Player,下面是我的代码 我可以通过编程方式创建mp3到.srt文件吗 holder.llView.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { MediaPlayer mediaP
holder.llView.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(filePathList.get(position));
mediaPlayer.prepare();
//mediaPlayer.addTimedTextSource(subTitleSrc, MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
//int textTrackIndex = findTrackIndexFor(
//MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT,
//mediaPlayer.getTrackInfo());
//if (textTrackIndex >= 0) {
// mediaPlayer.selectTrack(textTrackIndex);
//} else {
// Log.w("test", "Cannot find text track!");
//}
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// mediaPlayer.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
//@Override
//public void onTimedText(final MediaPlayer mediaPlayer, final TimedText timedText) {
//if (timedText != null) {
//Log.d("test", "subtitle: " + timedText.getText());
// }
// }
// });
// }
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
});
下面代码为ffmpeg命令
String cmd= "ffmpeg -i"+ filePathList.get(position).substring(filePathList.get(position).lastIndexOf("/")+1);
FFmpeg ffmpeg = FFmpeg.getInstance(CountryListActivity.this);
try {
// to execute "ffmpeg -version" command you just need to pass "-version"
ffmpeg.execute(new String[]{cmd}, new ExecuteBinaryResponseHandler() {
@Override
public void onStart() {
Log.e("!!!!!!!!!",""+filePathList.get(position));
}
@Override
public void onProgress(String message) {
Log.e("message..............",""+message);
}
@Override
public void onFailure(String message) {
Log.e("messageeeeeeeee..............",""+message);
}
@Override
public void onSuccess(String message) {
Log.e("messageeeeeeewwwwwwwwwwwee..............",""+message);
}
@Override
public void onFinish() {
Log.e("messagewwwewreeeeeeee..............","");
}
});
} catch (FFmpegCommandAlreadyRunningException e) {
// Handle if FFmpeg is already running
e.printStackTrace();
}
你基本上是在找什么,从语音到文本。正如您在问题中提到的,您已经录制了音频。您可以使用API从音频中获取文本。下面是来自Google的示例代码
// Imports the Google Cloud client library
import com.google.cloud.speech.v1.RecognitionAudio;
import com.google.cloud.speech.v1.RecognitionConfig;
import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
import com.google.cloud.speech.v1.RecognizeResponse;
import com.google.cloud.speech.v1.SpeechClient;
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1.SpeechRecognitionResult;
import com.google.protobuf.ByteString;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class QuickstartSample {
/**
* Demonstrates using the Speech API to transcribe an audio file.
*/
public static void main(String... args) throws Exception {
// Instantiates a client
try (SpeechClient speechClient = SpeechClient.create()) {
// The path to the audio file to transcribe
String fileName = "./resources/audio.raw";
// Reads the audio file into memory
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
ByteString audioBytes = ByteString.copyFrom(data);
// Builds the sync recognize request
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(AudioEncoding.LINEAR16)
.setSampleRateHertz(16000)
.setLanguageCode("en-US")
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setContent(audioBytes)
.build();
// Performs speech recognition on the audio file
RecognizeResponse response = speechClient.recognize(config, audio);
List<SpeechRecognitionResult> results = response.getResultsList();
for (SpeechRecognitionResult result : results) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcription: %s%n", alternative.getTranscript());
}
}
}
}
以便将其添加到您的Android项目中。要添加谷歌云,您可以直接下载,也可以直接下载。你需要创建你的功能或者使用一些第三方API,如果有的话。你能告诉我我可以使用哪种第三方API吗?String cmd=new String-I{filepath}{filenameRaw}.mp4-I{filepath}{filenameRaw}.mp4-I{filepath}{filenameRaw}.mp4-I{filepath}{filenameRaw}.mp4-filter\u complex\[0:v][1:v]hstack[top];[2:v][3:v]hstack[底部];[top][bottom]vstack\{filenamecomped}.mp4;应用程序默认凭据不可用。如果在谷歌计算引擎中运行,它们是可用的。否则,必须定义指向定义凭据的文件的环境变量GOOGLE\u APPLICATION\u CREDENTIALS。有关详细信息,请参阅。@VishalVaishnav I发布了相关代码,以将音频文件转换为文本。要设置凭据,您需要遵循文档。