This repository has been archived by the owner on Feb 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.rb
76 lines (64 loc) · 2.25 KB
/
process.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
require 'json'
require 'mysql2'
require 'open3'
require 'shellwords'
require 'yaml'
config = YAML.load_file File.join Dir.pwd, 'config.yml'
mysql = Mysql2::Client.new(
host: config['mysql']['host'],
username: config['mysql']['username'],
password: config['mysql']['password'],
database: config['mysql']['database']
)
def close_streams(*args)
args.each{|arg| arg.close}
end
upload_path = File.expand_path config['process']['upload_path']
`mkdir -p #{ upload_path }`
subjects = mysql.query <<-SQL
SELECT *
FROM manifest
WHERE processed=0
LIMIT 2000
SQL
total = subjects.count
count = 0
per_slice = 10
subjects.each_slice(per_slice).with_index do |subjects_slice, i|
puts "#{ (i + 1) * per_slice } / #{ total }"
# threads = []
subjects_slice.each do |subject|
# threads << Thread.new do
source_path = subject['source_path']
bson_id = subject['bson_id']
start_time = subject['start_time']
duration = subject['desired_duration']
subject['processed'] = 1
subject_path = File.join upload_path, "/subjects/"
`mkdir -p #{ subject_path }`
cmd = "ffmpeg -nostdin -ss #{ start_time } -i '#{ source_path }' -y -to #{ duration } -c:v libx264 -preset medium -crf 23 -vf scale=\"720:trunc(ow/a/2)*2\" -r 24 -pix_fmt yuv420p -threads 0 -an '#{ subject_path }#{ bson_id }.mp4'"
system cmd #, [:out, :err] => '/dev/null'
mysql.query <<-SQL
update manifest
set processed=1, processed_time='#{ Time.now }'
where bson_id='#{ bson_id }'
SQL
# end
end
# threads.each &:join
end
puts "Done."
# -nostdin disables standard input interaction
# -ss <position> when before -i will seek in the input file to position
# -i <filename> input
# -y overwrite output files without asking
# -to <position> stop writing output at position. must be time duration specification
# -c:v codec:stream_specifier selects an encoder when before an output file.
# libx264 is h.264 codec
# -preset medium is default and can be left out
# -crf (constant rate factor). Determines compression. 23 is default.
# -vf <filtergraph>. Create filtergraph specified and use it to filter stream
# -r force framerate to specified fps
# -pix_fmt sets pixel format.
# -threads number of encoding threads
# -an disable audio recording