0

I am trying to use command line tools to match the hash value that Plex.app computes for movie files (apparently, it's an SHA1 hash of the first block (4k) of a movie instead of the entire movie).

I tried using the following to get a hash using command line tools, but unsurprisingly the two hashes don't match.

file="/path/to/mp4"
dd bs=4k count=1 if="$file" | openssl sha1
1+0 records in
1+0 records out
4096 bytes transferred in 0.000012 secs (341333333 bytes/sec)
7785d5d6d611a27ac03dfdff8a2c89e9ffeffa3b
file="/path/to/mp4"
head -c 4096< "$file" | openssl sha1
7785d5d6d611a27ac03dfdff8a2c89e9ffeffa3b

Using ffprobe I realized that most of the data at the beginning of that mp4 file is the encoding settings and not the video stream. Made me think that whatever process Plex uses it probably avoids the encoding settings of the file since they can potentially be exactly the same.

Keeping in mind that there might be many reasons why the values don't match what would be the proper tools to attempt to duplicate their hash calculation.

2
  • (1) Your comment under the answer lets me suspect this may be an XY problem. Consider at least mentioning your goal in the question. (2) "Apparently, it's an SHA1 hash of the first block (4k) of a movie instead of the entire movie" – How do you know? How can you tell? Why is it apparent? Doesn't your try contradict this? I'm not trying to prove you wrong. I'm trying to avoid a situation where we assume you have researched something and we follow the wrong track. Aug 9, 2022 at 4:15
  • @KamilMaciorowski I disagree with your assessment. I laid out the bigger picture "why" in the first sentence. If my style of writing is not as analytical as you like - I am sorry for that. I use the word "apparently" because that information comes from another source - in any case it's not necessary to know how much data to hash or which digest to use in order to answer the question.
    – John
    Aug 9, 2022 at 13:58

2 Answers 2

3

You can do this directly using ffmpeg.

ffmpeg -hide_banner -v 0 -f data -start 0 -end 4096 -i subfile:input.mp4 -map 0 -f hash -hash sha160 -

This will read the input from byte 0 to the byte before offset 4096 and compute a SHA-1 (160b) hash.

The result is printed as

SHA160=55c306878d7a09e1c726310b49e7f63915346365

Note that a MP4 doesn't always have the header at the front. Also, the header contains a lot more than video encoding settings.

2
  • What typically would be in a header other than encoding settings?
    – John
    Aug 13, 2022 at 17:34
  • Media packets location, size, timestamps, string metadata, other bookkeeping data.
    – Gyan
    Aug 13, 2022 at 18:21
0

To do that, you need to find out how, exactly, the Plex app calculates this hash. You can find the Plex source on Github.

To better address your actual need, what is it that you're trying to achieve in the end? Maybe there's some easier way to get there.

1
  • Unfortunately it's proprietary C++, and the source on Github is no longer maintained
    – John
    Aug 9, 2022 at 14:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .