From: Calin Crisan <ccrisan@gmail.com>
Date: Sun, 17 Nov 2013 12:19:49 +0000 (+0200)
Subject: added a management command to recreate thumbnails
X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=b14bbb5229611d87ecc49ffb015fb6ee628eef05;p=motioneye-debian

added a management command to recreate thumbnails
---

diff --git a/doc/todo.txt b/doc/todo.txt
index 9049bfd..7abc86d 100644
--- a/doc/todo.txt
+++ b/doc/todo.txt
@@ -1,4 +1,3 @@
--> add a mgmt command for generating thumbnails
 -> make camera frames positions configurable
 -> add a view log functionality
 
diff --git a/motioneye.py b/motioneye.py
index ab54ab5..ad5eb9b 100755
--- a/motioneye.py
+++ b/motioneye.py
@@ -110,9 +110,7 @@ def _configure_settings():
             setattr(settings, name, next_arg)
         
         else:
-            print('unknown command line option: ' + arg)
-            _print_help()
-            sys.exit(-1)
+            return arg[2:]
     
     try:
         os.makedirs(settings.CONF_PATH)
@@ -131,7 +129,10 @@ def _print_help():
     print('Usage: ' + sys.argv[0] + ' [option1 value1] ...')
     print('available options: ')
     
-    for (name, value) in sorted(inspect.getmembers(settings)):  # @UnusedVariable
+    options = list(inspect.getmembers(settings))
+    options.append(('THUMBNAILS', None))
+    
+    for (name, value) in sorted(options):
         if name.upper() != name:
             continue
         
@@ -139,10 +140,38 @@ def _print_help():
             continue
         
         name = '--' + name.lower().replace('_', '-')
-        print('    ' + name)
+        if value is not None:
+            value = type(value).__name__
+        
+        line = '    ' + name
+        if value:
+            line += ' <' + value + '>'
+        print(line)
     
     print('')
+
+
+def _do_thumbnails():
+    import config
+    import mediafiles
+    
+    logging.info('recreating thumbnails for all video files...')
+    
+    for camera_id in config.get_camera_ids():
+        camera_config = config.get_camera(camera_id)
+        if camera_config.get('@proto') != 'v4l2':
+            continue
+        
+        logging.info('listing movie files for camera %(name)s' % {
+                'name': camera_config['@name']})
+        
+        target_dir = camera_config['target_dir']
+        
+        for full_path in mediafiles._list_media_files(target_dir, mediafiles._MOVIE_EXTS):
+            mediafiles.make_movie_preview(camera_config, full_path)
     
+    logging.info('done.')
+
 
 def _start_server():
     import server
@@ -221,10 +250,20 @@ def _start_movie_thumbnailer():
 
 
 if __name__ == '__main__':
-    _configure_settings()
+    cmd = _configure_settings()
     _configure_signals()
     _configure_logging()
     
+    if cmd:
+        if cmd == 'thumbnails':
+            _do_thumbnails()
+        
+        else:
+            print('unknown command line option: ' + cmd)
+            sys.exit(-1)
+        
+        sys.exit(0)
+    
     _start_motion()
     _start_cleanup()
     _start_movie_thumbnailer()
diff --git a/src/mediafiles.py b/src/mediafiles.py
index cb6a7ed..d5d73ff 100644
--- a/src/mediafiles.py
+++ b/src/mediafiles.py
@@ -109,13 +109,13 @@ def cleanup_media(media_type):
 
 
 def make_movie_preview(camera_config, full_path):
-    logging.debug('creating movie preview for %(path)s...' % {'path': full_path})
-    
     framerate = camera_config['framerate']
     pre_capture = camera_config['pre_capture']
     offs = pre_capture / framerate
-    offs = min(4, offs * 2)
+    offs = max(4, offs * 2)
     
+    logging.debug('creating movie preview for %(path)s with an offset of %(offs)s seconds ...' % {
+            'path': full_path, 'offs': offs})
 
     cmd = 'ffmpeg -i "%(path)s" -f mjpeg -vframes 1 -ss %(offs)s -y %(path)s.thumb' % {
             'path': full_path, 'offs': offs}