Various fixes to ugoira->webm converter
* Fix regex for getting frame data not working if agent is logged in to pixiv * Fix #unpack not returning the unpacked folder * Fix frame_data being inaccessible because it was a local variable * Fix images getting put in tmpdir, when they're expected to be in tmpdir/images * Fix deleting the tmpdir manually (it gets deleted automatically) * Fix output webm being put in tmpdir (which gets deleted) instead of write_path
This commit is contained in:
@@ -26,7 +26,7 @@ class PixivUgoiraConverter
|
|||||||
image_blob = file.get_input_stream.read
|
image_blob = file.get_input_stream.read
|
||||||
image = Magick::Image.from_blob(image_blob).first
|
image = Magick::Image.from_blob(image_blob).first
|
||||||
image.ticks_per_second = 1000
|
image.ticks_per_second = 1000
|
||||||
delay = frame_data[i]["delay"]
|
delay = @frame_data[i]["delay"]
|
||||||
rounded_delay = (delay_sum + delay).round(-1) - delay_sum.round(-1)
|
rounded_delay = (delay_sum + delay).round(-1) - delay_sum.round(-1)
|
||||||
image.delay = rounded_delay
|
image.delay = rounded_delay
|
||||||
delay_sum += delay
|
delay_sum += delay
|
||||||
@@ -41,7 +41,7 @@ class PixivUgoiraConverter
|
|||||||
Dir.mktmpdir do |tmpdir|
|
Dir.mktmpdir do |tmpdir|
|
||||||
FileUtils.mkdir_p("#{tmpdir}/images")
|
FileUtils.mkdir_p("#{tmpdir}/images")
|
||||||
folder.each_with_index do |file, i|
|
folder.each_with_index do |file, i|
|
||||||
path = File.join(tmpdir, file.name)
|
path = File.join(tmpdir, "images", file.name)
|
||||||
image_blob = file.get_input_stream.read
|
image_blob = file.get_input_stream.read
|
||||||
File.open(path, "wb") do |f|
|
File.open(path, "wb") do |f|
|
||||||
f.write(image_blob)
|
f.write(image_blob)
|
||||||
@@ -52,7 +52,7 @@ class PixivUgoiraConverter
|
|||||||
timecodes_path = File.join(tmpdir, "timecodes.tc")
|
timecodes_path = File.join(tmpdir, "timecodes.tc")
|
||||||
File.open(timecodes_path, "w+") do |f|
|
File.open(timecodes_path, "w+") do |f|
|
||||||
f.write("# timecode format v2\n")
|
f.write("# timecode format v2\n")
|
||||||
frame_data.each do |img|
|
@frame_data.each do |img|
|
||||||
f.write("#{delay_sum}\n")
|
f.write("#{delay_sum}\n")
|
||||||
delay_sum += img["delay"]
|
delay_sum += img["delay"]
|
||||||
end
|
end
|
||||||
@@ -61,9 +61,7 @@ class PixivUgoiraConverter
|
|||||||
|
|
||||||
ext = folder.first.name.match(/\.(.+)$/)[1]
|
ext = folder.first.name.match(/\.(.+)$/)[1]
|
||||||
system("ffmpeg -i #{tmpdir}/images/%06d.#{ext} -codec:v libvpx -crf 4 -b:v 5000k -an #{tmpdir}/tmp.webm")
|
system("ffmpeg -i #{tmpdir}/images/%06d.#{ext} -codec:v libvpx -crf 4 -b:v 5000k -an #{tmpdir}/tmp.webm")
|
||||||
system("mkvmerge -o #{tmpdir}/output.webm --timecodes 0:#{tmpdir}/timecodes.tc #{tmpdir}/tmp.webm")
|
system("mkvmerge -o #{write_path} --timecodes 0:#{tmpdir}/timecodes.tc #{tmpdir}/tmp.webm")
|
||||||
|
|
||||||
FileUtils.rm_rf(tmpdir)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -73,7 +71,7 @@ class PixivUgoiraConverter
|
|||||||
frame_path = File.join("pixiv_anim_#{pixiv_id}", "frame#{"%03d" % i}.png")
|
frame_path = File.join("pixiv_anim_#{pixiv_id}", "frame#{"%03d" % i}.png")
|
||||||
delay_path = File.join("pixiv_anim_#{pixiv_id}", "frame#{"%03d" % i}.txt")
|
delay_path = File.join("pixiv_anim_#{pixiv_id}", "frame#{"%03d" % i}.txt")
|
||||||
image_blob = file.get_input_stream.read
|
image_blob = file.get_input_stream.read
|
||||||
delay = frame_data[i]["delay"]
|
delay = @frame_data[i]["delay"]
|
||||||
image = Magick::Image.from_blob(image_blob).first
|
image = Magick::Image.from_blob(image_blob).first
|
||||||
image.format="PNG"
|
image.format="PNG"
|
||||||
image.write(frame_path)
|
image.write(frame_path)
|
||||||
@@ -88,11 +86,13 @@ class PixivUgoiraConverter
|
|||||||
end
|
end
|
||||||
|
|
||||||
def unpack(zipped_body)
|
def unpack(zipped_body)
|
||||||
Zip::CentralDirectory.new.read_from_stream(StringIO.new(zipped_body))
|
folder = Zip::CentralDirectory.new
|
||||||
|
folder.read_from_stream(StringIO.new(zipped_body))
|
||||||
|
folder
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_zipped_body
|
def fetch_zipped_body
|
||||||
zip_uri, frame_data = fetch_frames
|
zip_uri, @frame_data = fetch_frames
|
||||||
|
|
||||||
zip_body = Net::HTTP.start(zip_uri.host) do |http|
|
zip_body = Net::HTTP.start(zip_uri.host) do |http|
|
||||||
http.get(zip_uri.path, {"Referer" => "http://pixiv.net"}).body
|
http.get(zip_uri.path, {"Referer" => "http://pixiv.net"}).body
|
||||||
@@ -102,14 +102,14 @@ class PixivUgoiraConverter
|
|||||||
def fetch_frames
|
def fetch_frames
|
||||||
agent.get(url) do |page|
|
agent.get(url) do |page|
|
||||||
# Get the zip url and frame delay by parsing javascript contained in a <script> tag on the page.
|
# Get the zip url and frame delay by parsing javascript contained in a <script> tag on the page.
|
||||||
# Not an neat solution, but I haven't found any other location that has the frame delays listed.
|
# Not a neat solution, but I haven't found any other location that has the frame delays listed.
|
||||||
scripts = page.search("body script").find_all do |node|
|
scripts = page.search("body script").find_all do |node|
|
||||||
node.text =~ /_ugoira600x600\.zip/
|
node.text =~ /_ugoira600x600\.zip/
|
||||||
end
|
end
|
||||||
|
|
||||||
if scripts.any?
|
if scripts.any?
|
||||||
javascript = scripts.first.text
|
javascript = scripts.first.text
|
||||||
json = javascript.match(/;pixiv\.context\.ugokuIllustData\s+=\s+(\{.+\});(?:$|pixiv\.context)/)[1]
|
json = javascript.match(/;pixiv\.context\.ugokuIllustData\s+=\s+(\{.+?\});(?:$|pixiv\.context)/)[1]
|
||||||
data = JSON.parse(json)
|
data = JSON.parse(json)
|
||||||
zip_uri = URI(data["src"].sub("_ugoira600x600.zip", "_ugoira1920x1080.zip"))
|
zip_uri = URI(data["src"].sub("_ugoira600x600.zip", "_ugoira1920x1080.zip"))
|
||||||
frame_data = data["frames"]
|
frame_data = data["frames"]
|
||||||
|
|||||||
Reference in New Issue
Block a user