1
0
mirror of https://github.com/gryf/pygtktalog.git synced 2025-12-17 19:40:21 +01:00

added video module - replacement for midentify

added some mocks for images tests
This commit is contained in:
2009-04-07 19:42:44 +00:00
parent 5e8c33f05a
commit c46d29a5bb
21 changed files with 319 additions and 0 deletions

170
src/lib/video.py Normal file
View File

@@ -0,0 +1,170 @@
"""
Project: pyGTKtalog
Description: Gather video file information, make "screenshot" with content
of the movie file. Uses external tools like mplayer and
ImageMagick tools (montage, convert).
Type: lib
Author: Roman 'gryf' Dobosz, gryf73@gmail.com
Created: 2009-04-04
"""
import os
import shutil
from tempfile import mkdtemp, mkstemp
import math
from misc import float_to_string
class Video(object):
"""Class for retrive midentify script output and put it in dict.
Usually there is no need for such a detailed movie/clip information.
Midentify script belongs to mplayer package.
"""
def __init__(self, filename):
"""Init class instance. Filename of a video file is required."""
self.filename = filename
self.tags = {}
output = os.popen('midentify "%s"' % self.filename).readlines()
attrs = {'ID_VIDEO_WIDTH': ['width', int],
'ID_VIDEO_HEIGHT': ['height', int],
'ID_LENGTH': ['length', lambda x: int(x.split(".")[0])],
# length is in seconds
'ID_DEMUXER': ['container', str],
'ID_VIDEO_FORMAT': ['video_format', str],
'ID_VIDEO_CODEC': ['video_codec', str],
'ID_AUDIO_CODEC': ['audio_codec', str],
'ID_AUDIO_FORMAT': ['audio_format', str],
'ID_AUDIO_NCH': ['audio_no_channels', int],}
for line in output:
line = line.strip()
for attr in attrs:
if attr in line:
self.tags[attrs[attr][0]] = \
attrs[attr][1](line.replace("%s=" % attr, ""))
if 'length' in self.tags:
if self.tags['length'] > 0:
hours = self.tags['length'] / 3600
seconds = self.tags['length'] - hours * 3600
minutes = seconds / 60
seconds -= minutes * 60
length_str = "%02d:%02d:%02d" % (hours, minutes, seconds)
self.tags['duration'] = length_str
def capture(self, out_width=1024):
"""
Extract images for given video filename and montage it into one, big
picture, similar to output from Windows Media Player thing, but without
captions and time (who need it anyway?).
Arguments:
@out_width - width of generated image. If actual image width
exceeds this number scale is performed.
Returns: image filename or None
NOTE: You should remove returned file manually, or move it in some
other place, otherwise it stays in filesystem.
"""
if not (self.tags.has_key('length') or self.tags.has_key('width')):
return None
# Calculate number of pictures. Base is equivalent 72 pictures for
# 1:30:00 movie length
scale = int(10 * math.log(self.tags['length'], math.e) - 11)
no_pictures = self.tags['length'] / scale
if no_pictures > 8:
# for really short movies
no_pictures = (no_pictures / 8 ) * 8 # only multiple of 8, please.
if not no_pictures:
# movie too short or length is 0
return None
if no_pictures < 4:
no_pictures = 4
tempdir = mkdtemp()
file_desc, image_fn = mkstemp()
os.close(file_desc)
self.__make_captures(tempdir, no_pictures)
self.__make_montage(tempdir, image_fn, no_pictures, out_width)
shutil.rmtree(tempdir)
return image_fn
def __make_captures(self, directory, no_pictures):
"""
Make screens with mplayer into given directory
Arguments:
@directory - full output directory name
@no_pictures - number of pictures to take
"""
step = float(self.tags['length']/(no_pictures + 1))
current_time = 0
for dummy in range(1, no_pictures + 1):
current_time += step
time = float_to_string(current_time)
cmd = "mplayer \"%s\" -ao null -brightness 0 -hue 0 " \
"-saturation 0 -contrast 0 -vf-clr -vo jpeg:outdir=\"%s\" -ss %s" \
" -frames 1 2>/dev/null"
os.popen(cmd % (self.filename, directory, time)).readlines()
shutil.move(os.path.join(directory, "00000001.jpg"),
os.path.join(directory, "picture_%s.jpg" % time))
def __make_montage(self, directory, image_fn, no_pictures, out_width):
"""
Generate one big image from screnshots and optionally resize it.
Arguments:
@directory - source directory containing images
@image_fn - destination final image
@no_pictures - number of pictures
@out_width - width of final image to be scaled to.
"""
scale = False
row_length = 4
if no_pictures < 8:
row_length = 2
if (self.tags['width'] * row_length) > out_width:
scale = True
else:
for i in [8, 6, 5]:
if (no_pictures % i) == 0 and \
(i * self.tags['width']) <= out_width:
row_length = i
break
tile = "%dx%d" % (row_length, no_pictures / row_length)
_curdir = os.path.abspath(os.path.curdir)
os.chdir(directory)
# composite pictures
# readlines trick will make to wait for process end
cmd = "montage -tile %s -geometry +2+2 picture_*.jpg montage.jpg"
os.popen(cmd % tile).readlines()
# scale it to minimum 'modern' width: 1024
if scale:
cmd = "convert -scale %s montage.jpg montage_scaled.jpg"
os.popen(cmd % out_width).readlines()
shutil.move(os.path.join(directory, 'montage_scaled.jpg'),
image_fn)
else:
shutil.move(os.path.join(directory, 'montage.jpg'),
image_fn)
os.chdir(_curdir)
def __str__(self):
str_out = ''
for key in self.tags:
str_out += "%20s: %s\n" % (key, self.tags[key])
return str_out

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

View File

@@ -0,0 +1,44 @@
|== INFO ON TEST IMAGES ==|
Unless stated otherwise, all images come from Wikipedia Commons - http://commons.wikimedia.org
For author and license information please refer to their respective description pages.
All images were scaled down using the GIMP v 2.4.5 since all we care about is the Exif info.
Tested before and after scaling to ensure Exif data was not modified (except for 'software' field).
== Filename == == Wiki Filename ==
= Camera Makes and Models =
Canon_40D.jpg Iguana_iguana_male_head.jpg
Canon_40D_photoshop_import.jpg Anolis_equestris_-_bright_close_3-4.jpg
Canon_DIGITAL_IXUS_400.jpg Ducati749.jpg
Fujifilm_FinePix6900ZOOM.jpg Hylidae_cinerea.JPG
Fujifilm_FinePix_E500.jpg VlaamseGaaiVeertje1480.JPG
Kodak_CX7530.jpg Red-headed_Rock_Agama.jpg
Konica_Minolta_DiMAGE_Z3.jpg Knechtova01.jpg
Nikon_COOLPIX_P1.jpg Miyagikotsu-castle6861.JPG
Nikon_D70.jpg Anolis_carolinensis_brown.jpg
Olympus_C8080WZ.jpg Pterois_volitans_Manado-e.jpg
Panasonic_DMC-FZ30.jpg Rømø_-_St.Klement_-_Kanzel_3.jpg
Pentax_K10D.jpg Mrs._Herbert_Stevens_May_2008.jpg
Ricoh_Caplio_RR330.jpg Steveston_dusk.JPG
Samsung_Digimax_i50_MP3.jpg Villa_di_Poggio_a_Caiano,_sala_neoclassica_4.JPG
Sony_HDR-HC3.jpg Positive_roll_film.jpg
WWL_(Polaroid)_ION230.jpg PoudriereBoisSousRoche3.jpg
= Other Stuff =
long_description.jpg US_10th_Mountain_Division_soldiers_in_Afghanistan.jpg
Contributions :
PaintTool_sample.jpg -- Submitted by Jan Trofimov (OXIj)
If you come across an image which is incorrectly parsed or errors out, consider
contributing it to the test cases : ianare@gmail.com

105
src/test/unit/video_test.py Normal file
View File

@@ -0,0 +1,105 @@
"""
Project: pyGTKtalog
Description: Tests for Video class.
Type: test
Author: Roman 'gryf' Dobosz, gryf73@gmail.com
Created: 2008-12-15
"""
import unittest
import os
from lib.video import Video
class TestVideo(unittest.TestCase):
"""Class for retrive midentify script output and put it in dict.
Usually there is no need for such a detailed movie/clip information.
Video script belongs to mplayer package.
"""
def test_avi(self):
"""test mock avi file, should return dict with expected values"""
avi = Video("mocks/m.avi")
self.assertTrue(len(avi.tags) != 0, "result should have lenght > 0")
self.assertEqual(avi.tags['audio_format'], '85')
self.assertEqual(avi.tags['width'], 128)
self.assertEqual(avi.tags['audio_no_channels'], 2)
self.assertEqual(avi.tags['height'], 96)
self.assertEqual(avi.tags['video_format'], 'XVID')
self.assertEqual(avi.tags['length'], 4)
self.assertEqual(avi.tags['audio_codec'], 'mp3')
self.assertEqual(avi.tags['video_codec'], 'ffodivx')
self.assertEqual(avi.tags['duration'], '00:00:04')
self.assertEqual(avi.tags['container'], 'avi')
def test_avi2(self):
"""test another mock avi file, should return dict with expected
values"""
avi = Video("mocks/m1.avi")
self.assertTrue(len(avi.tags) != 0, "result should have lenght > 0")
self.assertEqual(avi.tags['audio_format'], '85')
self.assertEqual(avi.tags['width'], 128)
self.assertEqual(avi.tags['audio_no_channels'], 2)
self.assertEqual(avi.tags['height'], 96)
self.assertEqual(avi.tags['video_format'], 'H264')
self.assertEqual(avi.tags['length'], 4)
self.assertEqual(avi.tags['audio_codec'], 'mp3')
self.assertEqual(avi.tags['video_codec'], 'ffh264')
self.assertEqual(avi.tags['duration'], '00:00:04')
self.assertEqual(avi.tags['container'], 'avi')
def test_mkv(self):
"""test mock mkv file, should return dict with expected values"""
avi = Video("mocks/m.mkv")
self.assertTrue(len(avi.tags) != 0, "result should have lenght > 0")
self.assertEqual(avi.tags['audio_format'], '8192')
self.assertEqual(avi.tags['width'], 128)
self.assertEqual(avi.tags['audio_no_channels'], 2)
self.assertEqual(avi.tags['height'], 96)
self.assertEqual(avi.tags['video_format'], 'mp4v')
self.assertEqual(avi.tags['length'], 4)
self.assertEqual(avi.tags['audio_codec'], 'a52')
self.assertEqual(avi.tags['video_codec'], 'ffodivx')
self.assertEqual(avi.tags['duration'], '00:00:04')
self.assertEqual(avi.tags['container'], 'mkv')
def test_mpg(self):
"""test mock mpg file, should return dict with expected values"""
avi = Video("mocks/m.mpg")
self.assertTrue(len(avi.tags) != 0, "result should have lenght > 0")
self.assertFalse(avi.tags.has_key('audio_format'))
self.assertEqual(avi.tags['width'], 128)
self.assertFalse(avi.tags.has_key('audio_no_channels'))
self.assertEqual(avi.tags['height'], 96)
self.assertEqual(avi.tags['video_format'], '0x10000001')
self.assertFalse(avi.tags.has_key('lenght'))
self.assertFalse(avi.tags.has_key('audio_codec'))
self.assertEqual(avi.tags['video_codec'], 'ffmpeg1')
self.assertFalse(avi.tags.has_key('duration'))
self.assertEqual(avi.tags['container'], 'mpeges')
def test_ogm(self):
"""test mock ogm file, should return dict with expected values"""
avi = Video("mocks/m.ogm")
self.assertTrue(len(avi.tags) != 0, "result should have lenght > 0")
self.assertEqual(avi.tags['audio_format'], '8192')
self.assertEqual(avi.tags['width'], 160)
self.assertEqual(avi.tags['audio_no_channels'], 2)
self.assertEqual(avi.tags['height'], 120)
self.assertEqual(avi.tags['video_format'], 'H264')
self.assertEqual(avi.tags['length'], 4)
self.assertEqual(avi.tags['audio_codec'], 'a52')
self.assertEqual(avi.tags['video_codec'], 'ffh264')
self.assertEqual(avi.tags['duration'], '00:00:04')
self.assertEqual(avi.tags['container'], 'ogg')
def test_capture(self):
"""test capture with some small movie"""
avi = Video("mocks/m.avi")
filename = avi.capture()
self.assertTrue(filename != None)
self.assertTrue(os.path.exists(filename))
file_size = os.stat(filename)[6]
self.assertEqual(file_size, 9077)
os.unlink(filename)
if __name__ == "__main__":
unittest.main()