winlin

for #502, snapshot 1 image per 1 seconds, choose best from 5 images.

@@ -941,9 +941,21 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): @@ -941,9 +941,21 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin):
941 break 941 break
942 942
943 # how many snapshots to output. 943 # how many snapshots to output.
944 - vframes = 6 944 + vframes = 5
945 # the expire in seconds for ffmpeg to snapshot. 945 # the expire in seconds for ffmpeg to snapshot.
946 - expire = 10 946 + expire = 1
  947 + # the timeout to kill ffmpeg.
  948 + kill_ffmpeg_timeout = 30 * expire
  949 + # the ffmpeg binary path
  950 + ffmpeg = "./objs/ffmpeg/bin/ffmpeg"
  951 + # the best url for thumbnail.
  952 + besturl = os.path.join(static_dir, "%s/%s-best.png"%(snapshot['app'], snapshot['stream']))
  953 + # the lambda to generate the thumbnail with index.
  954 + lgo = lambda dir, app, stream, index: os.path.join(dir, "%s/%s-%03d.png"%(app, stream, index))
  955 + # the output for snapshot command
  956 + output = os.path.join(static_dir, "%s/%s-%%03d.png"%(snapshot['app'], snapshot['stream']))
  957 + # the ffmepg command to snapshot
  958 + cmd = '%s -i %s -vf fps=1 -vcodec png -f image2 -an -y -vframes %s -y %s'%(ffmpeg, url, vframes, output)
947 959
948 # already snapshoted and not expired. 960 # already snapshoted and not expired.
949 if process is not None and diff < expire: 961 if process is not None and diff < expire:
@@ -963,9 +975,8 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): @@ -963,9 +975,8 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin):
963 else: 975 else:
964 # guess the best one. 976 # guess the best one.
965 bestsize = 0 977 bestsize = 0
966 - besturl = os.path.join(static_dir, "%s/%s-best.png"%(snapshot['app'], snapshot['stream']))  
967 for i in range(0, vframes): 978 for i in range(0, vframes):
968 - output = os.path.join(static_dir, "%s/%s-%03d.png"%(snapshot['app'], snapshot['stream'], i + 1)) 979 + output = lgo(static_dir, snapshot['app'], snapshot['stream'], i + 1)
969 fsize = os.path.getsize(output) 980 fsize = os.path.getsize(output)
970 if bestsize < fsize: 981 if bestsize < fsize:
971 os.system("rm -f '%s'"%besturl) 982 os.system("rm -f '%s'"%besturl)
@@ -974,7 +985,7 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): @@ -974,7 +985,7 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin):
974 print 'the best thumbnail is %s'%besturl 985 print 'the best thumbnail is %s'%besturl
975 else: 986 else:
976 # wait for process to terminate, timeout is N*expire. 987 # wait for process to terminate, timeout is N*expire.
977 - if diff < 10 * expire: 988 + if diff < kill_ffmpeg_timeout:
978 continue 989 continue
979 # kill the process when user cancel. 990 # kill the process when user cancel.
980 else: 991 else:
@@ -982,9 +993,6 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): @@ -982,9 +993,6 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin):
982 print 'kill the process %s'%snapshot['cmd'] 993 print 'kill the process %s'%snapshot['cmd']
983 994
984 # create new process to snapshot. 995 # create new process to snapshot.
985 - ffmpeg = "./objs/ffmpeg/bin/ffmpeg"  
986 - output = os.path.join(static_dir, "%s/%s-%%03d.png"%(snapshot['app'], snapshot['stream']))  
987 - cmd = '%s -i %s -vf fps=1/6 -vcodec png -f image2 -an -y -vframes %s -y %s'%(ffmpeg, url, vframes, output)  
988 print 'snapshot by: %s'%cmd 996 print 'snapshot by: %s'%cmd
989 997
990 process = create_process(cmd, discard.fileno(), discard.fileno()) 998 process = create_process(cmd, discard.fileno(), discard.fileno())
@@ -994,7 +1002,7 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): @@ -994,7 +1002,7 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin):
994 pass; 1002 pass;
995 1003
996 # {"action":"on_publish","client_id":108,"ip":"127.0.0.1","vhost":"__defaultVhost__","app":"live","stream":"livestream"} 1004 # {"action":"on_publish","client_id":108,"ip":"127.0.0.1","vhost":"__defaultVhost__","app":"live","stream":"livestream"}
997 - # ffmpeg -i rtmp://127.0.0.1:1935/live?vhost=dev/stream -vf fps=1/6 -vcodec png -f image2 -an -y -vframes 3 -y static-dir/live/livestream-%03d.png 1005 + # ffmpeg -i rtmp://127.0.0.1:1935/live?vhost=dev/stream -vf fps=1 -vcodec png -f image2 -an -y -vframes 3 -y static-dir/live/livestream-%03d.png
998 def snapshot_create(self, req): 1006 def snapshot_create(self, req):
999 url = "rtmp://127.0.0.1/%s...vhost...%s/%s"%(req['app'], req['vhost'], req['stream']) 1007 url = "rtmp://127.0.0.1/%s...vhost...%s/%s"%(req['app'], req['vhost'], req['stream'])
1000 if url in self.__snapshots: 1008 if url in self.__snapshots: