Last active
July 11, 2017 17:16
-
-
Save marxjohnson/3febd710a7153c3f4d862ed1e3a5f796 to your computer and use it in GitHub Desktop.
Ubuntu Podcast of Yesteryear Original Unit Reproduction Script
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
# Usage: upyours.rb episode1.mp3 episode2.mp3 jingle.mp3 outputfile.mp3 | |
# License: https://opensource.org/licenses/MIT | |
duration = `ffprobe -v 0 -show_entries format=duration -of compact=p=0:nk=1 #{ARGV[0]}`.to_f # Get the duration of the first file | |
fingerprint = `fpcalc #{ARGV[2]} -length 10 -raw`.lines[2].split('=')[1].split(',') # Get the fingetprint of the third file | |
$lt1000 = false # Set a flag | |
$discrepencies = Hash.new() # Store discrepencies for each clip | |
((duration.to_i*10-(90*10))..(duration.to_i*10)).each do | offset | # Scan through the last 90 seconds of the episode, in 1/10s intervals | |
pos = (offset.to_f / 10) # Caluclate the starting position of the clip | |
`ffmpeg -y -t 10 -ss #{pos} -i #{ARGV[0]} /tmp/testclip.mp3 > /dev/null 2>&1` # Get a 10 second clip from the starting position | |
testprint = `fpcalc /tmp/testclip.mp3 -length 10 -raw`.lines[2].split('=')[1].split(',') # Get the fingerprint of the clip | |
discrepency = 0 | |
fingerprint.zip(testprint).to_h.each_pair do | finger, test | | |
# Compare the fingerprints by XORing each pair of numbers. The more bits that match, the closer the fingerprints are. | |
# Add up all the discrepencies (bits set to 1 after XOR) | |
discrepency += (finger.to_i ^ test.to_i).to_s(2).count("1") | |
end | |
if (!$lt1000 && discrepency < 1000) then $lt1000 = true; end # < 1000 is a pretty good match, we're nearly at the clip. | |
if ($lt1000 && discrepency > 1600) then break; end # We've gone past the clip, so stop scanning through. | |
$discrepencies[pos] = discrepency # Store the discrepency against the start position, and try the next clip. | |
end | |
cliplength = $discrepencies.key($discrepencies.values.min) # We want a clip from the start of the file to the position of the closest match (lowest discrepency) found above. | |
`ffmpeg -y -t #{cliplength} -i #{ARGV[0]} /tmp/start.mp3 | |
`ffmpeg -y -i "concat:/tmp/start.mp3|#{ARGV[1]}" -acodec copy #{ARGV[3]}` # Stick the clip onto the start of the second file. | |
File.delete('/tmp/start.mp3', '/tmp/testclip.mp3') # Delete the temporary files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment