for snapshot, choose the best to link to the [stream]-best.png
正在显示
1 个修改的文件
包含
30 行增加
和
7 行删除
| @@ -939,9 +939,14 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): | @@ -939,9 +939,14 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): | ||
| 939 | del self.__snapshots[url] | 939 | del self.__snapshots[url] |
| 940 | print 'abort snapshot %s'%snapshot['cmd'] | 940 | print 'abort snapshot %s'%snapshot['cmd'] |
| 941 | break | 941 | break |
| 942 | - | 942 | + |
| 943 | + # how many snapshots to output. | ||
| 944 | + vframes = 6 | ||
| 945 | + # the expire in seconds for ffmpeg to snapshot. | ||
| 946 | + expire = 10 | ||
| 947 | + | ||
| 943 | # already snapshoted and not expired. | 948 | # already snapshoted and not expired. |
| 944 | - if process is not None and diff < 10: | 949 | + if process is not None and diff < expire: |
| 945 | continue | 950 | continue |
| 946 | 951 | ||
| 947 | # terminate the active process | 952 | # terminate the active process |
| @@ -951,17 +956,35 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): | @@ -951,17 +956,35 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): | ||
| 951 | 956 | ||
| 952 | # None incidates the process hasn't terminate yet. | 957 | # None incidates the process hasn't terminate yet. |
| 953 | if process.returncode is not None: | 958 | if process.returncode is not None: |
| 954 | - # process terminated, check the returncode. | 959 | + # process terminated with error. |
| 955 | if process.returncode != 0: | 960 | if process.returncode != 0: |
| 956 | print 'process terminated with error=%s, cmd=%s'%(process.returncode, snapshot['cmd']) | 961 | print 'process terminated with error=%s, cmd=%s'%(process.returncode, snapshot['cmd']) |
| 962 | + # process terminated normally. | ||
| 963 | + else: | ||
| 964 | + # guess the best one. | ||
| 965 | + bestsize = 0 | ||
| 966 | + besturl = os.path.join(static_dir, "%s/%s-best.png"%(snapshot['app'], snapshot['stream'])) | ||
| 967 | + for i in range(0, vframes): | ||
| 968 | + output = os.path.join(static_dir, "%s/%s-%03d.png"%(snapshot['app'], snapshot['stream'], i + 1)) | ||
| 969 | + fsize = os.path.getsize(output) | ||
| 970 | + if bestsize < fsize: | ||
| 971 | + os.system("rm -f '%s'"%besturl) | ||
| 972 | + os.system("ln -sf '%s' '%s'"%(output, besturl)) | ||
| 973 | + bestsize = fsize | ||
| 974 | + print 'the best thumbnail is %s'%besturl | ||
| 957 | else: | 975 | else: |
| 976 | + # wait for process to terminate, timeout is N*expire. | ||
| 977 | + if diff < 10 * expire: | ||
| 978 | + continue | ||
| 958 | # kill the process when user cancel. | 979 | # kill the process when user cancel. |
| 959 | - process.kill() | 980 | + else: |
| 981 | + process.kill() | ||
| 982 | + print 'kill the process %s'%snapshot['cmd'] | ||
| 960 | 983 | ||
| 961 | # create new process to snapshot. | 984 | # create new process to snapshot. |
| 962 | ffmpeg = "./objs/ffmpeg/bin/ffmpeg" | 985 | ffmpeg = "./objs/ffmpeg/bin/ffmpeg" |
| 963 | - output = os.path.join(static_dir, "%s-%s-%%3d.png"%(snapshot['app'], snapshot['stream'])) | ||
| 964 | - cmd = '%s -i %s -vf fps=1/6 -vcodec png -f image2 -an -y -vframes 3 -y %s'%(ffmpeg, url, output) | 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) | ||
| 965 | print 'snapshot by: %s'%cmd | 988 | print 'snapshot by: %s'%cmd |
| 966 | 989 | ||
| 967 | process = create_process(cmd, discard.fileno(), discard.fileno()) | 990 | process = create_process(cmd, discard.fileno(), discard.fileno()) |
| @@ -971,7 +994,7 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): | @@ -971,7 +994,7 @@ class SrsWorker(cherrypy.process.plugins.SimplePlugin): | ||
| 971 | pass; | 994 | pass; |
| 972 | 995 | ||
| 973 | # {"action":"on_publish","client_id":108,"ip":"127.0.0.1","vhost":"__defaultVhost__","app":"live","stream":"livestream"} | 996 | # {"action":"on_publish","client_id":108,"ip":"127.0.0.1","vhost":"__defaultVhost__","app":"live","stream":"livestream"} |
| 974 | - # 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-%3d.png | 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 |
| 975 | def snapshot_create(self, req): | 998 | def snapshot_create(self, req): |
| 976 | url = "rtmp://127.0.0.1/%s...vhost...%s/%s"%(req['app'], req['vhost'], req['stream']) | 999 | url = "rtmp://127.0.0.1/%s...vhost...%s/%s"%(req['app'], req['vhost'], req['stream']) |
| 977 | if url in self.__snapshots: | 1000 | if url in self.__snapshots: |
-
请 注册 或 登录 后发表评论