Last active
April 8, 2016 16:14
-
-
Save agrathwohl/fba6906e8242836dc8c488e88a0f7bf7 to your computer and use it in GitHub Desktop.
Small ruby class that uses FFmpeg to perform PSNR analysis on two video files.
This file contains hidden or 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
| require "pathname" | |
| require "open3" | |
| class CompareVideo | |
| def initialize(reference, video) | |
| ref = Pathname.new(File.open("#{reference}", "r")) | |
| vid = Pathname.new(File.open("#{video}", "r")) | |
| @comp = [vid, ref] # Video first, reference file second | |
| end | |
| def compare(tech=['psnr','ssim']) | |
| @res = {} | |
| tech.each do |niq| | |
| puts "TASK: " + niq | |
| stdout, stderr, status = Open3.capture3('ffmpeg', '-y', '-i', | |
| "#{@comp[1]}", '-i', | |
| "#{@comp[0]}", '-filter_complex', | |
| "#{niq}", 'outputfile.mp4') | |
| puts "STATUS: " + (status.success? ? 'succeeded' : 'failed') | |
| @res[niq] = stderr | |
| end | |
| end | |
| def analyze | |
| psnr_avg = @res['psnr'].scan(/average:\d+.?\d+/).to_s.delete! 'average:' | |
| psnr = psnr_avg.gsub(/[\"\[\]]/, "").to_f | |
| puts "PSNR score: " + psnr.to_s | |
| ssim_res = @res['ssim'].scan(/All:\d+.?\d+/).to_s.delete! 'All:' | |
| ssim = ssim_res.gsub(/[\"\[\]]/, "").to_f | |
| puts "SSIM response: " + ssim.to_s | |
| return [psnr, ssim] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment