Sto utilizzando l'API di YouTube per caricare video pezzo per pezzo (vedere il codice seguente). Tuttavia, il caricamento a volte fallisce con file più grandi (+ 1 GB), ma non sempre. Il caricamento è mostrato come completo ma possono essere riprodotti solo un paio di minuti e il resto viene troncato. Ho fatto some research ma senza successo apparente. Le mie domande ora:L'API YouTube tronca i video [PHP]
- C'è la possibilità di contattare direttamente YouTube (vedere i registri di ciò che sta realmente accadendo)?
- Si tratta di un problema di codifica?
- L'errore può essere rilevato/rilevato tramite l'API (al momento non viene generata alcuna eccezione)
- Questo può accadere se si caricano video diversi contemporaneamente (in parallelo, cioè)?
- Qualcun altro ha riscontrato questo problema?
Qualsiasi aiuto/guida nella giusta direzione è molto apprezzato. Mi piacerebbe anche chiamare fuori una taglia di 500 punti come questo sono io facendo impazzire (appena fatto che ...)
Appendice: Lo script è eseguito su una riga di comando, attraverso un Gearman Server, con set_time_limit(0);
set . Il codice/funzione è solo un estratto (e funziona alla grande con file più piccoli, a volte anche fino a 10 GB).
EDIT: Secondo aergistal e di commenti di GeorgeQ sopra, ho cambiato il ciclo while per leggere pezzi direttamente (senza feof()
più) e salvare lo stato nel database.
/*
Uploads one file to youtube chunk by chunk
*/
function uploadFile($dbfile) {
$client = $this->client;
$youtube = new Google_Service_YouTube($client);
$htmlBody = "";
try {
// Create a snippet with title, description, tags and category ID
// Create an asset resource and set its snippet metadata and type.
// This example sets the video's title, description, keyword tags, and
// video category.
$snippet = new Google_Service_YouTube_VideoSnippet();
$snippet->setTitle("SO Example");
// Numeric video category. See
// https://developers.google.com/youtube/v3/docs/videoCategories/list
$snippet->setCategoryId("22");
// Set the video's status to "public". Valid statuses are "public",
// "private" and "unlisted".
$status = new Google_Service_YouTube_VideoStatus();
$status->privacyStatus = "private";
// Associate the snippet and status objects with a new video resource.
$video = new Google_Service_YouTube_Video();
$video->setSnippet($snippet);
$video->setStatus($status);
// Specify the size of each chunk of data, in bytes. Set a higher value for
// reliable connection as fewer chunks lead to faster uploads. Set a lower
// value for better recovery on less reliable connections.
$chunkSizeBytes = 1 * 1024 * 1024;
// Setting the defer flag to true tells the client to return a request which can be called
// with ->execute(); instead of making the API call immediately.
$client->setDefer(true);
// Create a request for the API's videos.insert method to create and upload the video.
$insertRequest = $youtube->videos->insert("status,snippet", $video);
// Create a MediaFileUpload object for resumable uploads.
$media = new Google_Http_MediaFileUpload(
$client,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes);
$media->setFileSize(filesize($dbfile->localfile));
// Read the media file and upload it chunk by chunk.
$status = false;
$handle = fopen($dbfile->localfile, "rb");
while (!$status && ($chunk = (fread($handle, $chunkSizeBytes))) !== FALSE) {
$status = $media->nextChunk($chunk);
$data = array("filename" => $dbfile->localfile, "status" => print_r($status, true));
$db->saveLog($data);
}
/* the old code
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
*/
fclose($handle);
// If you want to make other calls after the file upload, set setDefer back to false
$client->setDefer(false);
$log = array("success" => true, "snippet_id" => $status["id"]);
} catch (Google_ServiceException $e) {
$log = array("success" => false, "errormsg" => $e->getMessage());
} catch (Google_Exception $e) {
$log = array("success" => false, "errormsg" => $e->getMessage());
}
return $log;
}
come sono in esecuzione il sceneggiatura? Attraverso un browser o una riga di comando? – DaImTo
@DaImTo Riga di comando, server Gearman. Ho cambiato la domanda per riflettere le informazioni aggiuntive. – Jan
Come nota a margine [Perché è male usare feof() per controllare un loop] (http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). È anche possibile creare un ciclo infinito se il puntatore del file è per qualche motivo non valido. – aergistal