Ich verwende die YouTube-API, um Videos Stück für Stück hochzuladen (siehe Code unten). Der Upload schlägt jedoch manchmal mit größeren Dateien (+ 1 GB) fehl, aber nicht immer. Der Upload wird als abgeschlossen angezeigt, aber nur ein paar Minuten können gespielt werden und der Rest wird abgeschnitten. Ich tat some research, aber ohne erkennbaren Erfolg. Meine Frage (n) jetzt:YouTube-API schneidet Videos ab [PHP]
- Gibt es eine Möglichkeit, YouTube direkt zu kontaktieren (Protokolle zu sehen, was wirklich vor sich geht)?
- Ist das ein Codierungsproblem?
- Kann der Fehler über die API abgefangen/entdeckt werden (im Moment wird keine Ausnahme ausgelöst)
- Kann dies passieren, wenn Sie verschiedene Videos auf einmal hochladen (parallel dazu)?
- Hat jemand anderes dieses Problem festgestellt?
Jede Hilfe/Führung in die richtige Richtung wird sehr geschätzt. Ich würde sogar eine Prämie von 500 Punkten rufen, da dies mir wahnsinnig (genau das getan ...)
Anhang: Das Skript in einer Befehlszeile ausgeführt wird, durch einen Gearman Server, mit set_time_limit(0);
Sets . Der Code/Funktion ist nur ein Auszug (und läuft super mit kleineren Dateien, manchmal sogar bis zu 10 GB).
EDIT: Nach aergistal der und GeorgeQ Kommentare von oben, ich habe die while-Schleife geändert Stücke direkt lesen (keine feof()
mehr) und den Status in der Datenbank zu speichern.
/*
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;
}
, wie Sie den Lauf werden: es ein YT-Server Problem
@DaImTo Befehlszeile, Gearman-Server. Ich habe die Frage geändert, um die zusätzlichen Informationen widerzuspiegeln. – Jan
Als eine Randnotiz [Warum es schlecht ist, feof() zu verwenden, um eine Schleife zu steuern] (http://stackoverflow.com/questions/5431941/why-is-which-feof-file-always-wrong). Sie können sogar eine Endlosschleife erstellen, wenn der Dateizeiger aus irgendeinem Grund ungültig ist. – aergistal