【发布时间】:2011-05-25 03:22:00
【问题描述】:
是否可以从视频 URL 中获取缩略图?我需要在列表视图中显示视频的缩略图。
【问题讨论】:
-
@user3726986 是 ContentProvider Url 还是 youtube 之类的 Url?
标签: android
是否可以从视频 URL 中获取缩略图?我需要在列表视图中显示视频的缩略图。
【问题讨论】:
标签: android
是的,可以使用 ThumbnailUtils 获取视频的缩略图。
FileOutputStream out;
File land=new File(Environment.getExternalStorageDirectory().getAbsoluteFile()
+"/portland.jpg");// image file use to create image u can give any path.
Bitmap bitmap=ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);//filePath is your video file path.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
out=new FileOutputStream(land.getPath());
out.write(byteArray);
out.close();
【讨论】:
如果您专门询问youtube 视频,那么 Youtube 会自动生成 4 张图片。
http://img.youtube.com/vi/video_url_here/0.jpg
http://img.youtube.com/vi/video_url_here/1.jpg
http://img.youtube.com/vi/video_url_here/2.jpg
http://img.youtube.com/vi/video_url_here/3.jpg
标准图片尺寸
播放器背景缩略图(480x360 像素) http://i1.ytimg.com/vi/G0wGs3useV8/0.jpg
开始缩略图(120x90 像素) http://i1.ytimg.com/vi/G0wGs3useV8/1.jpg
中间缩略图(120x90 像素) http://i1.ytimg.com/vi/G0wGs3useV8/2.jpg
结束缩略图(120x90 像素) http://i1.ytimg.com/vi/G0wGs3useV8/3.jpg
高品质缩略图(480x360 像素) http://i1.ytimg.com/vi/G0wGs3useV8/hqdefault.jpg
中等质量缩略图(320x180 像素) http://i1.ytimg.com/vi/G0wGs3useV8/mqdefault.jpg
普通质量缩略图(120x90 像素) http://i1.ytimg.com/vi/G0wGs3useV8/default.jpg
标准清晰度缩略图(640x480 像素) http://i1.ytimg.com/vi/G0wGs3useV8/sddefault.jpg
最大分辨率缩略图(1920x1080 像素) http://i1.ytimg.com/vi/G0wGs3useV8/maxresdefault.jpg
【讨论】:
它在我的情况下工作
Uri videoUri = data.getData();
String selectedPathVideo="";
selectedPathVideo = ImageFilePath.getPath(getApplicationContext(), videoUri);
Log.i("Image File Path", ""+selectedPathVideo);
try {
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(selectedPathVideo, MediaStore.Video.Thumbnails.MICRO_KIND);
imgFarmerVideo.setImageBitmap(thumb);
} catch (Exception e) {
e.printStackTrace();
}
支持文件
public class ImageFilePath {
/**
* Method for return file path of Gallery image
*
* @param context
* @param uri
* @return path of the selected image file from gallery
*/
public static String getPath(final Context context, final Uri uri)
{
//check here to KITKAT or new version
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}
【讨论】:
尝试ThumbnailUtils,从文件路径获取视频位图
ThumbnailUtils.createVideoThumbnail(filePath,MediaStore.Video.Thumbnails.MINI_KIND);
【讨论】:
可以从视频url获取视频缩略图。
如果您的视频类型是 Vimeo,请按照以下步骤操作:
1)假设您的视频网址是: http://player.vimeo.com/video/17314292
2) 从 Url 获取视频 ID:例如。 17314292
3) 使用 Video id 和 XML 格式调用 WS http://vimeo.com/api/v2/video/17314292.xml
您将从视频 ID 获得视频缩略图的 xml 格式的“thumbnail_small”、“thumbnail_medium”、“thumbnail_large”
如果您的视频类型是 YouTube,请按照以下步骤操作:
1) 视频网址: http://www.youtube.com/embed/Nl2iMF0yKW8
2) 从 Url 获取视频 ID:例如。 Nl2iMF0yKW8
3) 替换此网址中的视频 ID http://i3.ytimg.com/vi/Nl2iMF0yKW8/default.jpg
注意:只需在此处替换您的 id。
【讨论】:
识别问题的组成部分非常重要。我实际上会说你有两个不同的问题:
从 URL 下载文件。
从不完整的视频文件创建缩略图。
分别看这两个,你会发现很多信息。
【讨论】:
要从 URL 中获取缩略图,到目前为止我只有一个解决方案, 你必须使用This library
支持文件、http、https、mms、mmsh和rtmp协议
和
支持aac、acc+、avi、flac、mp2、mp3、mp4、ogg、3gp等!格式(音频和视频):
【讨论】:
可以使用FFMPEG从视频文件或网址中获取缩略图。
FFMPEG 必须使用 NDK 构建(或者您可以找到一些 Android 构建的 FFMPEG 二进制文件)。这是一个构建 FFMPEG 的 GitHub 项目:
https://github.com/guardianproject/android-ffmpeg
然后,您可以将 FFMPEG 二进制文件包含在您的应用中,并从应用中的代码执行 FFMPEG,以使用以下命令从视频(本地或 URL)生成图像:
ffmpeg -i videosite.com/video.flv -ss 00:00:15.000 -vframes 1 thumbnail_out.png
需要考虑的一点是,您在什么搜索时间(选项 -ss)抓取缩略图,使其成为代表视频的有意义的图像。
【讨论】: