Skip to content

Commit

Permalink
Detect mp3 by byte signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxPower15 committed Feb 11, 2024
1 parent 77e8643 commit 0c3177b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,18 @@
input_file = "out/#{SINE_WAVE_FILE_NAME}"
end

if input_file.end_with?(".mp3")
f = File.open(input_file)
first_three_bytes_in_hex = f.read(3).unpack1("H*")
first_two_bytes_in_hex = first_three_bytes_in_hex[0..3]
f.close

# Byte signatures taken from https://en.wikipedia.org/wiki/List_of_file_signatures
looks_like_mp3 = first_three_bytes_in_hex == "494433" ||
first_two_bytes_in_hex == "fffb" ||
first_two_bytes_in_hex == "fff3" ||
first_two_bytes_in_hex == "fff2"

if looks_like_mp3
# Something about mp3s make it so they have extra padding between them when
# split. Remuxing to mkv fixes it.
#
Expand Down

0 comments on commit 0c3177b

Please sign in to comment.