[分享]Nautilus下用mplayer生成视频缩略图的方法

Totem,mplayer,sopcast,realplayer,bmp
回复
moderaterain
帖子: 97
注册时间: 2008-12-31 23:16

[分享]Nautilus下用mplayer生成视频缩略图的方法

#1

帖子 moderaterain » 2009-12-12 9:43

大家知道,nautilus默认使用totem-video-thumbnail来生成缩略图,如果你卸载了totem的话就没有视频缩略图用了。目前很多人使用mplayer作为视频播放软件,这里提供一个用mplayer生成视频缩略图的方法。

国外原贴见这里:
http://forums.fedoraforum.org/showthread.php?t=204321

这实际上是一个脚本文件,该方法利用mplayer的文件输出功能来生成缩略图,但默认是生成jpg文件,再使用convert命令转换为png图片,使用convert需要安装imagemagic中。实际上mplayer完全可以直接输出png图片,从而省略转换这一步骤,因此我将该脚本进行了如下修改:


以下内容用于生成缩略图,保存为mplayer-video-thumb.sh

代码: 全选

#!/bin/bash 

if [ ! -x "/usr/bin/mplayer" ] ; then
	exit 1
fi

PREFIX=mvideothumb
TMP=$(mktemp -d /tmp/$PREFIX.XXXXXX)
SIZE=128
FILE="$1"
OFILE="$2"

if (($# != 2)); then
	echo "$0 <in-file> <out-file>"
	exit
fi

FILE=$(echo -e "${FILE//\%/\\x}")

eval $(/usr/bin/mplayer \
	-really-quiet \
	-nojoystick \
	-nolirc \
	-nocache \
	-nortc \
	-noautosub \
	-vo null \
	-nosound \
	-frames 1 \
	-identify "$FILE" \
	| grep "^ID_LENGTH")

ID_LENGTH=${ID_LENGTH%%.*}

# 有的时候mplayer并不能返回正确的时间长度,故每次生成缩略图失败就将时间减半,最多进行3次尝试。

thumb_done=0
cnt=0

while ((!thumb_done)); do
	ID_LENGTH=$((ID_LENGTH/2))
	/usr/bin/mplayer \
		-ss "$ID_LENGTH" \
		-really-quiet \
		-nojoystick \
		-nolirc \
		-nocache \
		-nortc \
		-noautosub \
		-vf scale \
		-vo png:outdir=$TMP:z=3 \
		-nosound \
		-frames 1 \
		-zoom \
		-xy "$SIZE" \
		-slave "$FILE"
	if mv $TMP/00000001.png "$OFILE" || ((cnt > 2)); then
		thumb_done=1
	fi
	((cnt++))
done
rm -rf $TMP

以下内容保存用于配置nautilus,保存为gconf.sh

代码: 全选

#!/bin/bash
#这里指定mplayer-video-thumb.sh的位置
script_location=/usr/bin/mplayer-video-thumb.sh

if [ -e "$script_location" ]; then
    for i in $(gconftool-2 --all-dirs "/desktop/gnome/thumbnailers" \
            | grep "/video") \
            /desktop/gnome/thumbnailers/application@ogg \
            /desktop/gnome/thumbnailers/application@smil \
            /desktop/gnome/thumbnailers/application@vnd.rn-realmedia \
            /desktop/gnome/thumbnailers/application@vnd.rn-realvideo \
            /desktop/gnome/thumbnailers/application@x-extension-m4a \
            /desktop/gnome/thumbnailers/application@x-extension-mp4 \
            /desktop/gnome/thumbnailers/application@x-flash-video \
            /desktop/gnome/thumbnailers/application@x-matroska \
            /desktop/gnome/thumbnailers/application@x-ms-asf \
            /desktop/gnome/thumbnailers/application@x-ogg \
            /desktop/gnome/thumbnailers/application@x-quicktime-media-link \
            /desktop/gnome/thumbnailers/application@x-shockwave-flash \
            /desktop/gnome/thumbnailers/application@x-shorten \
            /desktop/gnome/thumbnailers/application@x-smil \
            /desktop/gnome/thumbnailers/application@xspf@xml \
            /desktop/gnome/thumbnailers/audio@x-pn-realaudio \
            /desktop/gnome/thumbnailers/image@vnd.rn-realpix \
            /desktop/gnome/thumbnailers/image@vnd.rn-realpix \
            /desktop/gnome/thumbnailers/misc@ultravox; do

        echo "Seting Key for ${i##*/}"
        /usr/bin/gconftool-2 -s "$i"/command -t string "$script_location %u %o"
        /usr/bin/gconftool-2 -s "$i"/enable -t boolean 'true'
    done
else
    echo "Unable to find: $script_location"
    echo "setup not complete"
    exit 1
fi
echo "SUCCESS!"
安装方法:

代码: 全选

chmod +x mplayer-video-thumb.sh gconf.sh
sudo cp mplayer-video-thumb.sh /usr/bin/
./gconf.sh
rm -r ~/.thumbnails/fail
然后刷新一下应该就可以看到视频缩略图了。附件提供文件打包下载,及自动安装脚本。

补充说明:
1.gconf.sh并没有配置所有的视频预览,对于能播放而不能生成缩略图的视频,可以手工修改其缩略图方式(gonf-editor->desktop->gnome->thumbnailers);
2.用mplayer来生成缩略图并不一定比totem快;
附件
mplayer-thumb.tar.gz
这个是附件
(1.58 KiB) 已下载 205 次
上次由 moderaterain 在 2009-12-12 10:37,总共编辑 1 次。
头像
jxhow
帖子: 5859
注册时间: 2008-10-24 22:02
来自: 浙江

Re: [分享]Nautilus下用mplayer生成视频缩略图的方法

#2

帖子 jxhow » 2009-12-12 10:21

mark
还是看文件名省事
http://sb.google.com/ 提问前 请在右上角搜索一下
头像
quanru
帖子: 336
注册时间: 2009-11-25 11:24

Re: [分享]Nautilus下用mplayer生成视频缩略图的方法

#3

帖子 quanru » 2010-12-04 16:21

我现在什么图片.视频的缩略图都不能显示 怎么办
望青年不怨天尤人不贪图安逸
头像
jepen
帖子: 221
注册时间: 2010-06-14 1:08

Re: [分享]Nautilus下用mplayer生成视频缩略图的方法

#4

帖子 jepen » 2010-12-04 23:05

quanru 写了:我现在什么图片.视频的缩略图都不能显示 怎么办

/usr/bin/下有mplayer吗 进ubuntu-tweak里的《文件管理器》设置一下试试 相比totem 这个能生成预览更多预览图
多谢LZ
头像
allfoxwy
帖子: 9
注册时间: 2010-05-03 11:46

Re: [分享]Nautilus下用mplayer生成视频缩略图的方法

#5

帖子 allfoxwy » 2012-05-08 6:32

感谢楼主的分享,我昨晚弄这个弄了一个通宵,对楼主做以下补充:
1、请不要使用 mplayer 直接输出 png ,而应该就像原贴一样输出 jpeg 然后用 convert 转换,原因是 mplayer 的 png 输出设备只支持 24bpp RGB 或者 BGR 的像素格式,而有大量的影片都是用 YUV 像素格式输出,这会导致 png 输出设备不能启动。
2、新的 Gnome3 已经不再使用 gconf 了,所以想要用自定义缩略图生成器就不能修改 gconf 了,正确的做法是在 /usr/share/thumbnailers 建立一个描述缩略图生成器的文件,我的描述文件是 /usr/share/thumbnailers/mplayer.thumbnailer :

代码: 全选

[Thumbnailer Entry]
TryExec=/home/allfox/programs/gnome_thumbnailer_with_mplayer
Exec=/home/allfox/programs/gnome_thumbnailer_with_mplayer %u %o %s
MimeType=application/vnd.rn-realmedia;application/mxf;application/ogg;application/ram;application/sdp;application/vnd.ms-wpl;application/x-extension-m4a;application/x-extension-mp4;application/x-flash-video;application/x-matroska;application/x-netshow-channel;application/x-ogg;application/x-quicktimeplayer;application/x-shorten;image/vnd.rn-realpix;image/x-pict;misc/ultravox;text/x-google-video-pointer;video/3gpp;video/dv;video/fli;video/flv;video/mp2t;video/mp4;video/mp4v-es;video/mpeg;video/msvideo;video/ogg;video/quicktime;video/vivo;video/vnd.divx;video/vnd.rn-realvideo;video/vnd.vivo;video/webm;video/x-anim;video/x-avi;video/x-flc;video/x-fli;video/x-flic;video/x-flv;video/x-m4v;video/x-matroska;video/x-mpeg;video/x-ms-asf;video/x-ms-asx;video/x-msvideo;video/x-ms-wm;video/x-ms-wmv;video/x-ms-wmx;video/x-ms-wvx;video/x-nsv;video/x-ogm+ogg;video/x-theora+ogg;video/x-totem-stream;audio/x-pn-realaudio;audio/3gpp;audio/ac3;audio/AMR;audio/AMR-WB;audio/basic;audio/midi;audio/mp2;audio/mp4;audio/mpeg;audio/ogg;audio/prs.sid;audio/vnd.rn-realaudio;audio/x-aiff;audio/x-ape;audio/x-flac;audio/x-gsm;audio/x-it;audio/x-m4a;audio/x-matroska;audio/x-mod;audio/x-mp3;audio/x-mpeg;audio/x-ms-asf;audio/x-ms-asx;audio/x-ms-wax;audio/x-ms-wma;audio/x-musepack;audio/x-pn-aiff;audio/x-pn-au;audio/x-pn-wav;audio/x-pn-windows-acm;audio/x-realaudio;audio/x-real-audio;audio/x-sbc;audio/x-speex;audio/x-tta;audio/x-wav;audio/x-wavpack;audio/x-vorbis;audio/x-vorbis+ogg;audio/x-xm;application/x-flac;
3、新的 Gnome3 在传递文件路径的时候会使用 URI ,所以会含有 escaped 的字符,需要对这些字符解码后才能输入 mplayer ,我自己的缩略图生成器是用 perl 写的 /home/allfox/programs/gnome_thumbnailer_with_mplayer :

代码: 全选

#!/usr/bin/env perl
use strict;
use 5.010;

# allfox's GNOME thumbnailer with MPlayer

use File::Temp qw(tempdir);
use List::Util qw(min);
use File::Spec qw();
use URI::Escape qw(uri_unescape);

sub thumbnailer
{
	my($mplayer) = `which mplayer`;
	chomp $mplayer;
	my($input, $output, $size) = @_;
	$input = uri_unescape($input);
	$output = uri_unescape($output);
	if($input =~ /\.part/i)
	{
		return;
	}
	my $common_arguments = '-nosound -noautosub -really-quiet';
	my $video_information = `$mplayer -identify -frames 0 $common_arguments '$input'`;
	$video_information =~ /^ID_VIDEO_WIDTH=(?<width>[0-9.]+)
				.*
				^ID_VIDEO_HEIGHT=(?<height>[0-9.]+)
				.*
				^ID_LENGTH=(?<length>[0-9.]+)
				/msix;
	my $width = $+{'width'} / 1;
	my $height = $+{'height'} / 1;
	my $length = $+{'length'} / 1;
	if(defined $size)
	{
		if($width > $height)
		{
			my $scale_to = min $width, $size;
			$width = $scale_to;
			$height = $height * ($scale_to / $width);
		}
		else
		{
			my $scale_to = min $height, $size;
			$width = $width * ($scale_to / $height);
			$height = $scale_to;
		}
	}
	my($temp_frames) = tempdir('gnome_thumbnailer_with_mplayer-XXXXXXXXXXXX', TMPDIR => 1, CLEANUP => 1);
	for(1..3)
	{
		$length /= 2;
		if(0 == system "$mplayer -vo jpeg:outdir='$temp_frames' -ss $length -frames 1 $common_arguments '$input'")
		{
			last;
		}
	}
	my $convert = `which convert`;
	chomp $convert;
	opendir TEMP_FRAMES, $temp_frames;
	for(readdir TEMP_FRAMES)
	{
		if(/^\d+/)
		{
			my $jpeg_result = File::Spec->catfile($temp_frames, $_);
			system "$convert $jpeg_result -thumbnail ${width}x$height png:'$output'";
			last;
		}
	}
	closedir TEMP_FRAMES;
}

&thumbnailer(@ARGV);
——今天也像有目的似的在漫步。
jindawei
帖子: 15
注册时间: 2012-02-12 0:32

Re: [分享]Nautilus下用mplayer生成视频缩略图的方法

#6

帖子 jindawei » 2012-12-26 0:01

allfoxwy 写了:感谢楼主的分享,我昨晚弄这个弄了一个通宵,对楼主做以下补充:
1、请不要使用 mplayer 直接输出 png ,而应该就像原贴一样输出 jpeg 然后用 convert 转换,原因是 mplayer 的 png 输出设备只支持 24bpp RGB 或者 BGR 的像素格式,而有大量的影片都是用 YUV 像素格式输出,这会导致 png 输出设备不能启动。
2、新的 Gnome3 已经不再使用 gconf 了,所以想要用自定义缩略图生成器就不能修改 gconf 了,正确的做法是在 /usr/share/thumbnailers 建立一个描述缩略图生成器的文件,我的描述文件是 /usr/share/thumbnailers/mplayer.thumbnailer :

代码: 全选

[Thumbnailer Entry]
TryExec=/home/allfox/programs/gnome_thumbnailer_with_mplayer
Exec=/home/allfox/programs/gnome_thumbnailer_with_mplayer %u %o %s
MimeType=application/vnd.rn-realmedia;application/mxf;application/ogg;application/ram;application/sdp;application/vnd.ms-wpl;application/x-extension-m4a;application/x-extension-mp4;application/x-flash-video;application/x-matroska;application/x-netshow-channel;application/x-ogg;application/x-quicktimeplayer;application/x-shorten;image/vnd.rn-realpix;image/x-pict;misc/ultravox;text/x-google-video-pointer;video/3gpp;video/dv;video/fli;video/flv;video/mp2t;video/mp4;video/mp4v-es;video/mpeg;video/msvideo;video/ogg;video/quicktime;video/vivo;video/vnd.divx;video/vnd.rn-realvideo;video/vnd.vivo;video/webm;video/x-anim;video/x-avi;video/x-flc;video/x-fli;video/x-flic;video/x-flv;video/x-m4v;video/x-matroska;video/x-mpeg;video/x-ms-asf;video/x-ms-asx;video/x-msvideo;video/x-ms-wm;video/x-ms-wmv;video/x-ms-wmx;video/x-ms-wvx;video/x-nsv;video/x-ogm+ogg;video/x-theora+ogg;video/x-totem-stream;audio/x-pn-realaudio;audio/3gpp;audio/ac3;audio/AMR;audio/AMR-WB;audio/basic;audio/midi;audio/mp2;audio/mp4;audio/mpeg;audio/ogg;audio/prs.sid;audio/vnd.rn-realaudio;audio/x-aiff;audio/x-ape;audio/x-flac;audio/x-gsm;audio/x-it;audio/x-m4a;audio/x-matroska;audio/x-mod;audio/x-mp3;audio/x-mpeg;audio/x-ms-asf;audio/x-ms-asx;audio/x-ms-wax;audio/x-ms-wma;audio/x-musepack;audio/x-pn-aiff;audio/x-pn-au;audio/x-pn-wav;audio/x-pn-windows-acm;audio/x-realaudio;audio/x-real-audio;audio/x-sbc;audio/x-speex;audio/x-tta;audio/x-wav;audio/x-wavpack;audio/x-vorbis;audio/x-vorbis+ogg;audio/x-xm;application/x-flac;
3、新的 Gnome3 在传递文件路径的时候会使用 URI ,所以会含有 escaped 的字符,需要对这些字符解码后才能输入 mplayer ,我自己的缩略图生成器是用 perl 写的 /home/allfox/programs/gnome_thumbnailer_with_mplayer :

代码: 全选

#!/usr/bin/env perl
use strict;
use 5.010;

# allfox's GNOME thumbnailer with MPlayer

use File::Temp qw(tempdir);
use List::Util qw(min);
use File::Spec qw();
use URI::Escape qw(uri_unescape);

sub thumbnailer
{
	my($mplayer) = `which mplayer`;
	chomp $mplayer;
	my($input, $output, $size) = @_;
	$input = uri_unescape($input);
	$output = uri_unescape($output);
	if($input =~ /\.part/i)
	{
		return;
	}
	my $common_arguments = '-nosound -noautosub -really-quiet';
	my $video_information = `$mplayer -identify -frames 0 $common_arguments '$input'`;
	$video_information =~ /^ID_VIDEO_WIDTH=(?<width>[0-9.]+)
				.*
				^ID_VIDEO_HEIGHT=(?<height>[0-9.]+)
				.*
				^ID_LENGTH=(?<length>[0-9.]+)
				/msix;
	my $width = $+{'width'} / 1;
	my $height = $+{'height'} / 1;
	my $length = $+{'length'} / 1;
	if(defined $size)
	{
		if($width > $height)
		{
			my $scale_to = min $width, $size;
			$width = $scale_to;
			$height = $height * ($scale_to / $width);
		}
		else
		{
			my $scale_to = min $height, $size;
			$width = $width * ($scale_to / $height);
			$height = $scale_to;
		}
	}
	my($temp_frames) = tempdir('gnome_thumbnailer_with_mplayer-XXXXXXXXXXXX', TMPDIR => 1, CLEANUP => 1);
	for(1..3)
	{
		$length /= 2;
		if(0 == system "$mplayer -vo jpeg:outdir='$temp_frames' -ss $length -frames 1 $common_arguments '$input'")
		{
			last;
		}
	}
	my $convert = `which convert`;
	chomp $convert;
	opendir TEMP_FRAMES, $temp_frames;
	for(readdir TEMP_FRAMES)
	{
		if(/^\d+/)
		{
			my $jpeg_result = File::Spec->catfile($temp_frames, $_);
			system "$convert $jpeg_result -thumbnail ${width}x$height png:'$output'";
			last;
		}
	}
	closedir TEMP_FRAMES;
}

&thumbnailer(@ARGV);
按你说的建立了/usr/share/thumbnailers/mplayer.thumbnailer文件 也把你的perl代码建立了对应的文件 然后应该怎么做? 手动执行一下perl文件?
头像
jfxwc
帖子: 152
注册时间: 2006-10-29 14:06

Re: [分享]Nautilus下用mplayer生成视频缩略图的方法

#7

帖子 jfxwc » 2013-01-15 22:44

额, linux 下暂时不会,在 win 下我用脚本已经做得很好。

继续学用 linux 吧。
kimminhee
帖子: 52
注册时间: 2011-03-29 19:32

Re: [分享]Nautilus下用mplayer生成视频缩略图的方法

#8

帖子 kimminhee » 2013-01-17 12:16

2013-01-177.png
2013-01-176.png
2013-01-173.png
附件
2013-01-171.png
2013-01-17.png
回复