Created
November 9, 2018 02:16
-
-
Save labocho/45c35256a78fdcaec23de8f861a3a05f to your computer and use it in GitHub Desktop.
Convert QVR Pro Playback API response (with gop_mode=1) to .264 file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Convert QVR Pro Playback API response (with gop_mode=1) to .264 file. | |
# Usage: ./q264to264 in.q264 > out.264 | |
def assert(cond) | |
raise unless cond | |
end | |
def i32(f) | |
f.read(4).unpack("I<")[0] | |
end | |
def i64(f) | |
f.read(8).unpack("Q<")[0] | |
end | |
def each_frame(path) | |
open(path) {|f| | |
f.read(3) # skip response status | |
while true | |
$stderr.puts "-" * 40 | |
$stderr.puts "position: 0x#{f.pos.to_s(16)}" | |
assert(f.readline == "\n") | |
$stderr.puts "timestamp: #{f.readline}" | |
$stderr.puts "size: #{f.readline}" | |
assert(i32(f) == 0) | |
lastframe = case i32(f) | |
when 10 | |
false | |
when 11 | |
true | |
else | |
raise | |
end | |
assert(i32(f) == 2) | |
assert(f.read(4) == "q264") | |
assert(i32(f) == 1280) # width | |
assert(i32(f) == 960) # height | |
assert(f.read(24) == "\x00" * 24) | |
assert(f.read(4) == "G726") | |
assert(i32(f) == 8000) | |
assert(i32(f) == 4) | |
assert(i32(f) == 1) | |
assert(i32(f) == 0) | |
size = i64(f) | |
yield f.read(size) | |
break if lastframe | |
end | |
} | |
end | |
each_frame(ARGV.first) do |frame| | |
$stdout.write(frame) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment