KoblentsBlog Photography
Contact About
Ches
Thumbnail Sheet From Video
Wanted to make a perl script that would generate a thumbnail sheet from a video. It seems that there are all sorts of tools out there that embed their own garbage onto the sheet - like watermarks - this doesn't.
Since this thrashes the filesystem, I put it on a ramdisk first, and mounted it on /tmp/thumbcreate.
Code after the jump. It's a bit of a prototype stage, but it's quite mature. Perl script:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
#!/usr/bin/perl

use strict;

my @files;
my $dir = '.';

# Option 1: pipe in file list to script
if (! -t STDIN) { @files = <>; }

# Option 2: give script a directory or a list of file names
if (scalar(@files) == 0 && scalar(@ARGV) > 0) {
	if (-d $ARGV[0]) { $dir = $ARGV[0]; } else { @files = @ARGV; }
}

# Option 2/3: if no files given, then use supplied or default directory
if (scalar(@files) == 0) {
	my $cmd = 'find ' . $dir . ' ' .
		'-maxdepth 1 ' .
		'-name "*.mp4" ' .
		'-o -name "*.avi" ' .
		'-o -name "*.mkv" ' .
		'-o -name "*.wmv" ' .
		'-o -name "*.m4v" ' .
		'-o -name "*.rm" ' .
		'-o -name "*.flv"';
	@files = `$cmd`;
}

my $img_w = 3; my $img_h = 8; my $img_tot = $img_w * $img_h;
my $width = 360;

my $sec_cmd; my $sec;
my $frame_cmd; my $stitch_cmd;
my $step_dur; my $frame_at; my $extr_cnt;

my $dir_tmp = "/tmp/thumbcreate"; system("mkdir -p $dir_tmp");
my $dir_tgt = "thumbs"; system("mkdir -p $dir_tgt");
foreach (@files) {
	chomp;
	$sec_cmd = "ffprobe -i \"$_\" -show_entries format=duration " .
		"-v quiet -of csv=\"p=0\"";
	$sec = `$sec_cmd`; chomp($sec);

	$step_dur = $sec / $img_tot;
	$step_dur =~ s/\..*//;

	for ($frame_at = $step_dur, $extr_cnt = 0;
			$frame_at < $sec;
			$frame_at += $step_dur, $extr_cnt++) {
		$frame_cmd = "ffmpeg -hide_banner -loglevel panic " .
			"-accurate_seek -ss $frame_at.00 " .
			"-i \"$_\" -y -frames:v 1 " .
			"-filter_complex scale=$width:-1 " .
			"$dir_tmp/th_$extr_cnt.jpg";
		system($frame_cmd);
	}

	$stitch_cmd = "ffmpeg -hide_banner -loglevel panic " .
		"-i $dir_tmp/th_%d.jpg -y " .
		"-filter_complex tile=${img_w}x${img_h} \"$dir_tgt/$_.jpg\"";
	system($stitch_cmd);
}
Ches Koblents
June 4, 2017
 
« Newer Older »
© Copyright Koblents.com, 2012-2024