|
| 1 | +package ai.rev; |
| 2 | + |
| 3 | +import ai.rev.speechtotext.ApiClient; |
| 4 | +import ai.rev.speechtotext.models.NlpModel; |
| 5 | +import ai.rev.speechtotext.models.asynchronous.*; |
| 6 | +import ai.rev.speechtotext.models.vocabulary.CustomVocabulary; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +public class AsyncTranslateMediaUrl { |
| 15 | + |
| 16 | + public static void main(String[] args) { |
| 17 | + // Assign your access token to a String |
| 18 | + String accessToken = "<YOUR_ACCESS_TOKEN>"; |
| 19 | + |
| 20 | + // Initialize the ApiClient with your access token |
| 21 | + ApiClient apiClient = new ApiClient(accessToken); |
| 22 | + |
| 23 | + // Set up source configuration parameters |
| 24 | + String mediaUrl = "https://www.rev.ai/FTC_Sample_1.mp3"; |
| 25 | + |
| 26 | + // Initialize the RevAiJobOptions object and assign |
| 27 | + RevAiJobOptions revAiJobOptions = new RevAiJobOptions(); |
| 28 | + revAiJobOptions.setSourceConfig(mediaUrl, null); |
| 29 | + revAiJobOptions.setDeleteAfterSeconds(2592000); // 30 days in seconds |
| 30 | + revAiJobOptions.setLanguage("en"); |
| 31 | + revAiJobOptions.setTranslationOptions(new TranslationOptions(Arrays.asList( |
| 32 | + new TranslationLanguageOptions("es") |
| 33 | + .setModel(NlpModel.PREMIUM), |
| 34 | + new TranslationLanguageOptions("de")) |
| 35 | + )); |
| 36 | + |
| 37 | + RevAiJob submittedJob; |
| 38 | + |
| 39 | + try { |
| 40 | + // Submit job with transcription options |
| 41 | + submittedJob = apiClient.submitJobUrl(revAiJobOptions); |
| 42 | + } catch (IOException e) { |
| 43 | + throw new RuntimeException("Failed to submit url [" + mediaUrl + "] " + e.getMessage()); |
| 44 | + } |
| 45 | + String jobId = submittedJob.getJobId(); |
| 46 | + System.out.println("Job Id: " + jobId); |
| 47 | + System.out.println("Job Status: " + submittedJob.getJobStatus()); |
| 48 | + System.out.println("Created On: " + submittedJob.getCreatedOn()); |
| 49 | + |
| 50 | + // Waits 5 seconds between each status check to see if job is complete |
| 51 | + boolean isJobComplete = false; |
| 52 | + while (!isJobComplete) { |
| 53 | + RevAiJob retrievedJob; |
| 54 | + try { |
| 55 | + retrievedJob = apiClient.getJobDetails(jobId); |
| 56 | + } catch (IOException e) { |
| 57 | + throw new RuntimeException("Failed to retrieve job [" + jobId + "] " + e.getMessage()); |
| 58 | + } |
| 59 | + |
| 60 | + RevAiJobStatus retrievedJobStatus = retrievedJob.getJobStatus(); |
| 61 | + if (retrievedJobStatus == RevAiJobStatus.TRANSCRIBED |
| 62 | + || retrievedJobStatus == RevAiJobStatus.FAILED) { |
| 63 | + isJobComplete = true; |
| 64 | + } else { |
| 65 | + try { |
| 66 | + Thread.sleep(5000); |
| 67 | + } catch (InterruptedException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Waits 5 seconds between each status check to see if summarization job is complete |
| 74 | + isJobComplete = false; |
| 75 | + while (!isJobComplete) { |
| 76 | + RevAiJob retrievedJob; |
| 77 | + try { |
| 78 | + retrievedJob = apiClient.getJobDetails(jobId); |
| 79 | + } catch (IOException e) { |
| 80 | + throw new RuntimeException("Failed to retrieve job [" + jobId + "] " + e.getMessage()); |
| 81 | + } |
| 82 | + |
| 83 | + TranslationJobStatus translationJobStatus = retrievedJob.getTranslation().getTargetLanguages().get(0).getJobStatus(); |
| 84 | + if (translationJobStatus == TranslationJobStatus.COMPLETED |
| 85 | + || translationJobStatus == TranslationJobStatus.FAILED) { |
| 86 | + isJobComplete = true; |
| 87 | + } else { |
| 88 | + try { |
| 89 | + Thread.sleep(5000); |
| 90 | + } catch (InterruptedException e) { |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Get the transcript and caption outputs |
| 97 | + RevAiTranscript objectTranscript; |
| 98 | + String textTranscript; |
| 99 | + InputStream srtCaptions; |
| 100 | + InputStream vttCaptions; |
| 101 | + |
| 102 | + try { |
| 103 | + objectTranscript = apiClient.getTranslatedTranscriptObject(jobId, "es"); |
| 104 | + textTranscript = apiClient.getTranslatedTranscriptText(jobId, "es"); |
| 105 | + srtCaptions = apiClient.getTranslatedCaptions(jobId, "es", RevAiCaptionType.SRT, null); |
| 106 | + vttCaptions = apiClient.getTranslatedCaptions(jobId, "es", RevAiCaptionType.VTT, null); |
| 107 | + |
| 108 | + System.out.println("Translation:" + textTranscript); |
| 109 | + } catch (IOException e) { |
| 110 | + e.printStackTrace(); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + /* |
| 115 | + * The job can now be deleted. Deleting the job will remove ALL information |
| 116 | + * about the job from the Rev AI servers. Subsequent requests to Rev AI that |
| 117 | + * use the deleted jobs Id will return 404's. |
| 118 | + */ |
| 119 | + // apiClient.deleteJob(jobId); |
| 120 | + } |
| 121 | +} |
0 commit comments