我正在使用Slim framework为我的Android应用程序制作api。但是当我通过调用api从我的Android应用程序上传视频到本地服务器时,它会在我的应用程序中为视频生成白色缩略图。如何获取上传的缩略图视频。
我制作api的代码是:
$app->post('/do_upload', 'authentication', function () use ($app) {
verifyRequiredParams(array('albumId'));
global $user_id;
$response = array();
$file_path = "../uploads/$user_id/photo_gallery/thumbnails/";
if (!file_exists($file_path)) {
mkdir($file_path, 0777, true);
}
$thumbnail = $user_id . "/photo_gallery/thumbnails/";
$response = array();
if (isset($_FILES["files"])) {
$temp = explode(".", $_FILES['files']['name']);
$detectedType = $_FILES["files"]["name"];
$filetype = substr($detectedType, strpos($detectedType, ".") + 1);
$newfilename = 'shared_' . rand(1, 99999) . '.' . end($temp);
$file_path = $file_path . basename($newfilename);
$thumbnail = "$user_id/photo_gallery/thumbnails/" . $newfilename;
$filename = "$user_id/photo_gallery/thumbnails/" . $newfilename;
$mobile_thumbnail = "$user_id/photo_gallery/thumbnails/" . $newfilename;
if (move_uploaded_file($_FILES['files']['tmp_name'], $file_path)) {
}
$albumId = urldecode($app->request->post('albumId'));
$caption = urldecode($app->request->post('caption'));
$db = new DbHandler();
if (($filetype == "gif")
|| ($filetype == "png")
|| ($filetype == "jpeg")
|| ($filetype == "jpg")
|| ($filetype == "JPEG")
|| ($filetype == "PNG")
|| ($filetype == "GIF")
|| ($filetype == "JPG")
) {
$type = "image";
$result = $db->SharedImage($albumId, $user_id, $caption, $filename, $thumbnail, $mobile_thumbnail, $type);
} else {
$type = "video";
$result = $db->SharedImage($albumId, $user_id, $caption, $filename, $thumbnail, $mobile_thumbnail, $type);
}
if ($result != "null") {
while ($res = $result->fetch_assoc()) {
$response = array();
$response["id"] = $res["id"];
$response["event_id"] = $res["event_id"];
$response["album_id"] = $res["album_id"];
$response["status"] = $res["status"];
$response["filename"] = $res["filename"];
$response["contributedby_id"] = $res["contributedby_id"];
$response["guest_id"] = $res["guest_id"];
$response["thumbnail"] = $res["thumbnail"];
$response["mobile_thumbnail"] = $res["mobile_thumbnail"];
$response["order_id"] = $res["order_id"];
$response["created"] = $res["created"];
$response["updated"] = $res["updated"];
$response["title"] = $res["title"];
$response["type"] = $res["type"];
$response["caption"] = $res["caption"];
$response["taken"] = $res["taken"];
$response["copied_from"] = $res["copied_from"];
$response["media_size"] = $res["media_size"];
$response["thumbnail_size"] = $res["thumbnail_size"];
echoRespnse(200, $response);
}
} else {
$response["message"] = "response contain no data";
echoRespnse(200, $response["message"]);
}
} else {
$response ["error"] = true;
$response ["message"] = "Please select a file to upload";
echoRespnse(200, $response);
}
});
请告诉我为什么视频上传的缩略图是白色的,我怎样才能获得视频封面。
答案 0 :(得分:0)
使用ffmpeg,我的问题解决了。这是代码exec('ffmpeg -y -i '.$file_path.' -vcodec mjpeg -ss 00:00:01 -vframes 1 -an -s 620x440 -f rawvideo '.$image.'',$thumb_stdout, $retval);
这在路径$ image上创建了一个缩略图,大小为620 * 440。