Amazon Transcribe Streaming service request in PHP or Laravel? - php

I'm a PHP developer, I would like to use transcribe real time transcriptions, but I haven't found documentation for PHP about this tool, do you have a strength?
For audio translation, I found it, but I didn't find real time.
Below audio conversion example:
//Storage::disk('temp')->get('1.mp3');
// // https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-transcribe-2017-10-26.html
// https://docs.aws.amazon.com/transcribe/latest/dg/streaming.html
public function transcribeVoice(Request $request)
{
$client = AWS::createClient('transcribeService');
$path = 'https://audio-job.s3.us-east-2.amazonaws.com/audio.mp3';
try {
$result = $client->getTranscriptionJob([
'TranscriptionJobName' => 'audio-job'
]);
if ($result['TranscriptionJob']['TranscriptionJobStatus'] == 'IN_PROGRESS') {
return redirect('/')->with('status', 'Progressing now');
} else if ($result['TranscriptionJob']['TranscriptionJobStatus'] == 'COMPLETED') {
$file = file_get_contents($result['TranscriptionJob']['Transcript']['TranscriptFileUri']);
$json = json_decode($file);
$transcript = $json->results->transcripts[0]->transcript;
$client->deleteTranscriptionJob([
'TranscriptionJobName' => 'audio-job', // REQUIRED
]);
return redirect('/transcribeVoice')->with('result', $transcript);
}
} catch (Aws\TranscribeService\Exception\TranscribeServiceException $e) {
$result = $client->startTranscriptionJob([
'LanguageCode' => 'pt-BR', // REQUIRED
'Media' => [ // REQUIRED
'MediaFileUri' => $path,
],
'MediaFormat' => 'mp3', // REQUIRED
'TranscriptionJobName' => 'audio-job', // REQUIRED
]);
return redirect('/transcribeVoice')->with('status', 'Progressing now');
}
}
}
// StartStreamTranscription – Starts a bi-directional HTTP/2 stream where audio is streamed to Amazon Transcribe and the transcription results are streamed to your application.
// https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.TranscribeService.TranscribeServiceClient.html

Related

Uploading recording file stream to aws s3 in PHP is very slow

In my application I can record video and save it to aws s3 bucket using vueJS as front end and Laravel php as backend.
I am using ffmpeg to upload recording stream to s3 bucket.
1 min video taking 4 mins and
3 mins video taking 9 mins (Always not successfully uploaded, some times it fails)
Below is the code in backend.
public function video_upload(Request $request)
{
// Response Declaration
$response=array();
$response_code = 200;
$response['status'] = false;
$response['data'] = [];
// Validation
// TODO: Specify mimes:mp4,webm,ogg etc
$validator = Validator::make(
$request->all(), [
'file' => 'required',
]
);
if ($validator->fails()) {
$response['data']['validator'] = $validator->errors();
return response()->json($response);
}
try{
$file = $request->file('file');
//covert
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open($file);
$format = new X264();
$format->on('progress', function ($video, $format, $percentage) {
echo "$percentage % transcoded";
});
$video->save($format, 'output.mp4');
//end convert
$file_name = str_replace ('/', '', Hash::make(time())).'.mp4';
$file_folder = 'uploads/video/';
// Store the file to S3
// $store = Storage::disk('s3')->put($file_folder.$file_name, file_get_contents($file));
$store = Storage::disk('s3')->put($file_folder.$file_name, file_get_contents('output.mp4'));
if($store){
// Replace old file if exist
//delete the file from public folder
$file = public_path('output.mp4');
if (file_exists($file)) {
unlink($file);
}
if(isset($request->old_file)){
if(Storage::disk('s3')->exists($file_folder.basename($request->old_file))) {
Storage::disk('s3')->delete($file_folder.basename($request->old_file));
}
}
}
$response['status'] = true;
$response['data']= '/s3/'.$file_folder. $file_name;
}catch (\Exception $e) {
$response['data']['message']=$e->getMessage()."line".$e->getLine();
$response_code = 400;
}
return response()->json($response, $response_code);
}
I was researching on Transfer Acceleration and multipart upload but question is do i do from aws end or in backend.

Video uploaded through YouTube Data API is becoming private

I have tried uploading a video to YouTube channels using by the YouTube Data API, by composer:
"require": {
"google/apiclient": "^2.7",
"hybridauth/hybridauth": "~3.4.0",
"guzzlehttp/guzzle": "^7.0"
}
The video is uploaded successfully, but YouTube marks every video uploaded into it as private. Does anybody know how to fix this scenario?
public function upload_video_on_youtube($id, $arr_data)
{
$result_data = array();
$channel_id = $id;
$uploaded = false;
$stopper = 0;
while ($uploaded == false && $stopper == 0) {
$arr_data['summary'] = $this->getrandomstring(10);
$arr_data['title'] = $this->getrandomstring(10);
$client = new Google_Client();
$arr_token = $this->getAccessToken($channel_id);
if ($arr_token['error'] == false) {
$res = array();
$accessToken = array(
'access_token' => $arr_token['access_token']
);
$client->setAccessToken($accessToken);
$service = new Google_Service_YouTube($client);
$video = new Google_Service_YouTube_Video();
$videoSnippet = new Google_Service_YouTube_VideoSnippet();
$videoSnippet->setDescription($arr_data['summary']);
$videoSnippet->setTitle($arr_data['title']);
$video->setSnippet($videoSnippet);
$videoStatus = new Google_Service_YouTube_VideoStatus();
$videoStatus->setPrivacyStatus('unlisted');
$video->setStatus($videoStatus);
try {
$response = $service->videos->insert(
'snippet,status',
$video,
array(
'data' => file_get_contents($arr_data['video_path']),
'mimeType' => 'video/*',
'uploadType' => 'multipart'
)
);
if (isset($response->id)) {
$video_id = $response->id;
$res['error'] = false;
$res['response'] = $video_id;
array_push($result_data, $res);
$uploaded = true;
return $result_data;
}
} catch (Exception $e) {
if (401 == $e->getCode()) {
// echo ($arr_token['email'] . " Youtube Access token expired");
$refresh_token = $this->get_refersh_token($channel_id);
$client = new GuzzleHttp\Client(['base_uri' => 'https://accounts.google.com']);
$response = $client->request('POST', '/o/oauth2/token', [
'form_params' => [
"grant_type" => "refresh_token",
"refresh_token" => $refresh_token,
"client_id" => $arr_token['client_id'],
"client_secret" => $arr_token['client_secret'],
],
]);
$data = json_decode($response->getBody());
$data->refresh_token = $refresh_token;
$this->update_access_token($channel_id, json_encode($data));
$uploaded = false;
} elseif (403 == $e->getCode()) {
// echo ($arr_token['email'] . ' Youtube channel quota exceeded');
$channel_id = $channel_id + 1;
$uploaded = false;
}
}
} else if ($arr_token['error'] == true) {
$res['error'] = true;
$res['response'] = "Your Daily Upload Quota is Exceeded";
array_push($result_data, $res);
$stopper = 1;
return $result_data;
}
}
}
If you check the documentation for videos.insert you will see that all videos that are uploaded by apps that have not gone through the verification process will be set to private.
Once you go through the verification process you will be able to set your videos to public
This is an update after some clarity from Google.
At the top of the Video.insert page you will see it states.
Apps that dont need to be verified can just go though an audit is not verification, these are two different things. You need to apply for an audit. YouTube API Services - Audit and Quota Extension Form

Does AWS PHP SDK automatically retry multipart uploads?

Based on the sdk code, the s3 client code uses retry logic, but the sample code from the docs suggest doing a loop until the multipart upload finishes correctly.
$s3Client = new S3Client([
'profile' => 'default',
'region' => 'us-east-2',
'version' => '2006-03-01'
]);
$bucket = 'your-bucket';
$key = 'my-file.zip';
// Using stream instead of file path
$source = fopen('/path/to/large/file.zip', 'rb');
$uploader = new ObjectUploader(
$s3Client,
$bucket,
$key,
$source
);
do {
try {
$result = $uploader->upload();
if ($result["#metadata"]["statusCode"] == '200') {
print('<p>File successfully uploaded to ' . $result["ObjectURL"] . '.</p>');
}
print($result);
} catch (MultipartUploadException $e) {
rewind($source);
$uploader = new MultipartUploader($s3Client, $source, [
'state' => $e->getState(),
]);
}
} while (!isset($result));
Is that MultipartUploadException being thrown after the standard 3 retries for it have happened? Or are multipart uploads not covered by the retry policy?

Google Apiclient, Google Drive, after uploaded file empty

I upload file some formats and in google drive have file with empty body. Why not understand
I install google api client
"name": "google/apiclient",
"version": "1.1.7",
and try upload file in google dics
I get access token help with HWIO bundle, add scope
scope:
"https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive"
and get accesToken like this
{"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
"expires_in":3600, "id_token":"TOKEN", "created":1320790426}
beign I try upload exist file (doc, pdf, img)
$client = $this->get('artel.google.api.client');
/** #var CodeUserReference[] $accessToken */
$accessToken = $this->get('doctrine.orm.entity_manager')->getRepository('ArtelProfileBundle:CodeUserReference')
->getReferenceAccessClient($user);
if ($accessToken) {
$client->setAccessToken($accessToken[0]->getAccessTokenClient());
} else {
$view = $this->view(['No accessToken was found for this user id'], 400);
return $this->handleView($view);
}
$service = new \Google_Service_Drive($client->getGoogleClient());
$file = new \Google_Service_Drive_DriveFile();
$file->setName($request->request->get('title')
? $request->request->get('title')
: $request->files->get('file')->getClientOriginalName()
);
$file->setDescription($request->request->get('description'));
$file->setMimeType($request->request->get('mimeType')
? $request->request->get('mimeType')
: $request->files->get('file')->getClientMimeType()
);
// Set the parent folder.Google_Service_Drive_ParentReferen, this class not find in google/apiclient, version": "1.1.7", I don\'t know why..(
if ($request->request->get('parentId') != null) {
$parent = new Google_Service_Drive_ParentReferen();
$parent->setId($request->request->get('parentId'));
$file->setParents(array($parent));
}
try {
$data = $request->files->get('file');
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => $request->request->get('mimeType')
? $request->request->get('mimeType')
: $request->files->get('file')->getClientMimeType(),
'uploadType' => 'media'
));
// Uncomment the following line to print the File ID
// print 'File ID: %s' % $createdFile->getId();
return View::create()
->setStatusCode(200)
->setData([$createdFile]);
} catch (\Exception $e) {
$view = $this->view((array) $e->getMessage(), 400);
return $this->handleView($view);
}
have response
{
"internal_gapi_mappings": [],
"model_data": [],
"processed": [],
"collection_key": "spaces",
"capabilities_type": "Google_Service_Drive_DriveFileCapabilities",
"capabilities_data_type": "",
"content_hints_type": "Google_Service_Drive_DriveFileContentHints",
"content_hints_data_type": "",
"id": "0B2_i_Tc5Vr8UWV9Jc2psQkhqS3M",
"image_media_metadata_type": "Google_Service_Drive_DriveFileImageMediaMetadata",
"image_media_metadata_data_type": "",
"kind": "drive#file",
"last_modifying_user_type": "Google_Service_Drive_User",
"last_modifying_user_data_type": "",
"mime_type": "application/msword",
"name": "Resume — Symfony Backend Developer, PHP, Shuba Ivan.doc",
"owners_type": "Google_Service_Drive_User",
"owners_data_type": "array",
"permissions_type": "Google_Service_Drive_Permission",
"permissions_data_type": "array",
"sharing_user_type": "Google_Service_Drive_User",
"sharing_user_data_type": "",
"video_media_metadata_type": "Google_Service_Drive_DriveFileVideoMediaMetadata",
"video_media_metadata_data_type": ""
}
all I have in file this:
/tmp/phpaPpHBp
I use this doc v3 because in "name": "google/apiclient", "version": "1.1.7" no find function "insert",
So, mime_type correct but inside emty, only like this - /tmp/phpaPpHBp
I try some uploadType - media, multipart, resumable but stil empty
When try pdf mime_type = application/pdf but still emty
What I\'am doing wrong and why I have emty file in google drive?
anybody, who knows, help
add file_get_content and work fine :
if ($request->request->get('parentId') !== null) {
$parent = new \Google_Service_Drive_ParentReference();
$parent->setId($request->request->get('parentId'));
$file->setParents(array($parent));
}
$insertArray = [
'mimeType' => isset($fileUpload) ? $fileUpload->getClientMimeType() : $request->request->get('mimeType')
];
if (isset($fileUpload)) {
$insertArray ['uploadType'] = 'media';
$insertArray ['data'] = file_get_contents($fileUpload);
}
try {
$createdFile = $service->files->insert($file, $insertArray);
return View::create()
->setStatusCode(200)
->setData([$createdFile]);
} catch (\Exception $e) {
$view = $this->view((array) self::ERROR_OCCURRED . $e->getMessage(), 400);
return $this->handleView($view);
}

Google API PHP Update File

I'm trying to update the content of the file. Use the PHP function:
function updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) {
try {
// First retrieve the file from the API.
$file = $service->files->get($fileId);
// File's new metadata.
$file->setTitle($newTitle);
$file->setDescription($newDescription);
$file->setMimeType($newMimeType);
// File's new content.
$data = file_get_contents($newFileName);
$additionalParams = array(
'newRevision' => $newRevision,
'data' => $data,
'mimeType' => $newMimeType
);
// Send the request to the API.
$updatedFile = $service->files->update($fileId, $file, $additionalParams);
return $updatedFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
....
$data = retrieveAllFiles($service);
$fileName = 'test.txt';
$mimeType = mime_content_type('./'.$fileName);
$res = updateFile($service, $data[0]['id'], $data[0]['title'], 'update', $mimeType, $fileName, true);
I'm trying to add a text file line "test string". Function updates the data file (description, lastModifyingUser...), but the content of the file remains the same. Who can tell what's wrong?
In additionalParams need to add :
'uploadType' => 'multipart',
or
'uploadType' => 'media',
Hope it helps!

Categories