-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffmpeg_control.rb
113 lines (91 loc) · 2.78 KB
/
ffmpeg_control.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2011 Exavideo LLC.
#
# This file is part of Exadeck.
#
# Exadeck is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Exadeck is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Exadeck. If not, see <http://www.gnu.org/licenses/>.
require 'patchbay'
require 'json'
require_relative 'subprocess'
class FfmpegSubprocess < Subprocess
def initialize(title=nil)
@title = title
super
end
stderr /frame=\s*(\d+)/ do |match|
@frames_encoded = match[1]
end
stderr /fps=\s*(\d+)/ do |match|
@fps = match[1]
end
stderr /size=\s*(\d+\w+)/ do |match|
@size = match[1]
end
stderr /time=\s*(\S+)/ do |match|
@time = match[1]
end
stderr /bitrate=\s*(\S+)/ do |match|
@bitrate = match[1]
end
attr_reader :frames, :fps, :size, :time, :bitrate
attr_accessor :title
cmd 'ffmpeg -i /home/armena/openreplay2/test.mjpg -f rawvideo -s 1920x1080 - >/dev/null'
def json_status
{
:time => @time,
:bitrate => @bitrate,
:size => @size,
:fps => @fps,
:frames_encoded => @frames_encoded,
:title => @title
}.merge(super)
end
end
class ProcessControlApp < Patchbay
get '/processes' do
render :json => (@processes.map { |x| x.json_status }).to_json
end
get '/process/:id' do
render :json => @processes[params[:id].to_i].json_status.to_json
end
put '/process/:id/start' do
pr = @processes[params[:id].to_i]
pr.start
render :json => ''
end
put '/process/:id/stop' do
pr = @processes[params[:id].to_i]
# send SIGTERM, if process not dead after 5 seconds send SIGKILL
puts "sending TERM signal"
pr.kill("-TERM")
pr.wait(5)
if pr.is_running
puts "sending KILL signal"
pr.kill("-KILL")
pr.wait
else
puts "process died on its own"
end
render :json => ''
end
put '/process/:id' do
data = incoming_json
@processes[params[:id].to_i].cmd(data['cmd'])
render :json => ''
end
attr_accessor :processes
self.files_dir = 'public_html'
end
app = ProcessControlApp.new
app.processes = [ FfmpegSubprocess.new('Program'), FfmpegSubprocess.new('Multiviewer') ]
app.run