# What algorithm does plex use to get database hash value?

**URL:** <https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621>\
**Category:** General Discussions\
**Tags:** server-windows, server-mac, server-linux\
**Created:** [August 7, 2022, 10:37pm UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621 "2022-08-07T22:37:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_001](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/john_001/32/279863_2.png) [@John\_001](https://forums.plex.tv/u/John_001)\
**Post date:** [August 7, 2022, 10:37pm UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/1 "2022-08-07T22:37:22Z")

</div>

I am trying to match the file on the HD with the file listed in the sqlite database using the `hash` value. Does plex just hash the movie file or does it hash the entire directory? I’ve tried sha1, md5 and others with no luck finding a match for the movie listing in the .db

---

<div class="post-metadata">

**Author:** ![ChuckPa](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/chuckpa/32/79710_2.png) [@ChuckPa](https://forums.plex.tv/u/ChuckPa)\
**Post date:** [August 7, 2022, 11:22pm UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/2 "2022-08-07T23:22:20Z")

</div>

Plex reads the first block from the file (4K) and generates the hash from there.  
It doesn’t make sense to hash the entire file as that would be wasteful and resource intensive.

---

<div class="post-metadata">

**Author:** ![John\_001](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/john_001/32/279863_2.png) [@John\_001](https://forums.plex.tv/u/John_001)\
**Post date:** [August 8, 2022, 12:04am UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/3 "2022-08-08T00:04:57Z")

</div>

Thanks @ChuckPa !

Now I am searching for a simple way to do this on the command line… and not coming up with much.

```auto
file="path/to/mp4"

08/07/22_19:08:14 /Users/john
0-zsh% dd count=1 ibs=4k < "$file" | openssl sha1
1+0 records in
8+0 records out
4096 bytes transferred in 0.000015 secs (273066667 bytes/sec)
b024d1b7910d423fdb59b5687bcf97519f056771

```

However this does not match the hash in the database for that mp4. In fact it looks like most of the first 4k just has the encoding info.

EDIT  
tried changing language for terminal according to [this post](https://stackoverflow.com/questions/19242275/re-error-illegal-byte-sequence-on-mac-os-x)

```auto

08/07/22_19:31:42 /Users/john/
0-zsh% LC_CTYPE=C && LANG=C

08/07/22_19:34:01 /Users/john/
0-zsh% dd bs=1 count=4096 < "$file" > bytes 
4096+0 records in
4096+0 records out
4096 bytes transferred in 0.010991 secs (372669 bytes/sec)

08/07/22_19:34:11 /Users/john/
0-zsh% wc -m /Users/john/bytes 
    4096 /Users/john/bytes

08/07/22_19:34:20 /Users/john/
0-zsh% openssl sha1 /Users/john/bytes 
SHA1(/Users/john/bytes)= b024d1b7910d423fdb59b5687bcf97519f056771

```

Suggestions please!

ALSO,  
Is there a description somewhere of other `metadata_items` values in `~/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db`

Tried to figure out `duration` which can’t be in seconds… is it milliseconds?

---

<div class="post-metadata">

**Author:** ![ChuckPa](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/chuckpa/32/79710_2.png) [@ChuckPa](https://forums.plex.tv/u/ChuckPa)\
**Post date:** [August 8, 2022, 12:40am UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/4 "2022-08-08T00:40:21Z")

</div>

> [@John\_001](#):
>
> I am trying to match the file on the HD with the file listed in the sqlite database using the `hash` value.

There is a column in the path, named `file`, which contains the path to the file.

You’re looking for media\_parts table.

---

<div class="post-metadata">

**Author:** ![John\_001](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/john_001/32/279863_2.png) [@John\_001](https://forums.plex.tv/u/John_001)\
**Post date:** [August 8, 2022, 12:42am UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/5 "2022-08-08T00:42:35Z")

</div>

> [@ChuckPa](#):
>
> There is a column in the path, named `file`, which contains the path to the file.
> 
> You’re looking for media\_parts table.

I am trying to modify the date in the database to match the date of the file on the disk. For some reason Plex is not displaying files in order they were added. That is my ultimate purpose here.

---

<div class="post-metadata">

**Author:** ![ChuckPa](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/chuckpa/32/79710_2.png) [@ChuckPa](https://forums.plex.tv/u/ChuckPa)\
**Post date:** [August 8, 2022, 12:53am UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/6 "2022-08-08T00:53:32Z")

</div>

Plex’s method for storing dates is not any way you’d expect.  
It’s stored in seconds since epoch.

I would think it’s easier to `touch` a file and set the date fields than to modifiy the database

---

<div class="post-metadata">

**Author:** ![ChuckPa](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/chuckpa/32/79710_2.png) [@ChuckPa](https://forums.plex.tv/u/ChuckPa)\
**Post date:** [August 8, 2022, 12:56am UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/7 "2022-08-08T00:56:01Z")

</div>

> [@John\_001](#):
>
> For some reason Plex is not displaying files in order they were added.

Plex adds the file in the order presented by `readdir()`

---

<div class="post-metadata">

**Author:** ![John\_001](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/john_001/32/279863_2.png) [@John\_001](https://forums.plex.tv/u/John_001)\
**Post date:** [August 8, 2022, 1:11am UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/8 "2022-08-08T01:11:08Z")

</div>

I have that epoch bit figured out.

The files themselves all have the correct modification dates but for some reason plex is not displaying them in the order they were added. Not sure `touching` the files will cause plex to update.

---

<div class="post-metadata">

**Author:** ![John\_001](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/john_001/32/279863_2.png) [@John\_001](https://forums.plex.tv/u/John_001)\
**Post date:** [August 8, 2022, 10:03pm UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/9 "2022-08-08T22:03:39Z")

</div>

> [@ChuckPa](#):
>
> Plex reads the first block from the file (4K) and generates the hash from there.  
> It doesn’t make sense to hash the entire file as that would be wasteful and resource intensive.

Can you be more specific on how it generates the hash?

I have tried a number of methods on the command line and have failed to match the hash value that plex calculates.

Is it the video alone or the entire mp4 container that is hashed?

Encoding settings?

---

<div class="post-metadata">

**Author:** ![ChuckPa](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/chuckpa/32/79710_2.png) [@ChuckPa](https://forums.plex.tv/u/ChuckPa)\
**Post date:** [August 8, 2022, 10:44pm UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/10 "2022-08-08T22:44:35Z")

</div>

@John_001

As I said, they read the first block (4K) bytes.

This includes the header (media info) and the start of the video stream.

They have a routine which generates the hash. I can’t share it because it’s proprietary C++

---

<div class="post-metadata">

**Author:** ![ChuckPa](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/chuckpa/32/79710_2.png) [@ChuckPa](https://forums.plex.tv/u/ChuckPa)\
**Post date:** [August 8, 2022, 10:51pm UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/11 "2022-08-08T22:51:03Z")

</div>

> [@John\_001](#):
>
> I am trying to match the file on the HD with the file listed in the sqlite database using the `hash` value.

If I go back to your OP,

“Which file on the HD?”

I can do this:

```auto
<r/Library/Plex Media Server/Plug-in Support/Databases] # ../../../../Plex\ SQLite com.plexapp.plugins.library.db            
SQLite version 3.35.5 2021-04-19 18:32:05
Enter ".help" for usage hints.
sqlite> select file from media_parts;
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E01 - Pilot [HDTV-1080p AVC EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E02 - Sex for Furniture [HDTV-1080p AVC EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E03 - The Ring [HDTV-1080p AVC EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E04 - The Wood Pile [HDTV-1080p AVC EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E05 - The Garage Band [HDTV-1080p AVC EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E06 - Your Mother or Your Wife [HDTV-1080p AVC EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E07 - Dream Getaway [HDTV-1080p AVC EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E08 - Death Sex [WEBDL-1080p h264 EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E09 - The Toaster [DVD XviD MP3 2.0].avi
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E10 - Daddy's Girl [WEBDL-1080p h264 EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E11 - The Anniversary Party [WEBDL-1080p h264 EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E12 - The Hockey Lie [WEBDL-1080p h264 EAC3 5.1].mkv
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E13 - Fight Friend [DVD XviD MP3 2.0].avi
/share/CACHEDEV1_DATA/nas/glock/qa/340/tv/00s/'Til Death (2006)/Season 01/'Til Death (2006) - S01E14 - The Colleague [DVD XviD MP3 2.0].avi

```

There’s the path. You can then take the ID columns from other tables to here or back (for a few of them)

---

<div class="post-metadata">

**Author:** ![dane22](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/dane22/32/4389_2.png) [@dane22](https://forums.plex.tv/u/dane22)\
**Post date:** [August 8, 2022, 11:05pm UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/12 "2022-08-08T23:05:58Z")

</div>

Use the rest API

`http://<IP_OF_PMS>:32400/library/metadata/<MEDIA_KEY>/tree?X-Plex-Token=<YOUR_TOKEN>`

You can find your token here:  
[https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/](https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/)

Sample:

Issue:

> curl -k [https://192.168.1.2:32400/library/metadata/9038/tree?X-Plex-Token=ThisIsMyToken](https://192.168.1.2:32400/library/metadata/9038/tree?X-Plex-Token=ThisIsMyToken)

Response:

```auto
<?xml version="1.0" encoding="UTF-8"?>
<MediaContainer size="1" identifier="com.plexapp.plugins.library" mediaTagPrefix="/system/bundle/media/flags/" mediaTagVersion="1659032912">
<MetadataItem id="9038" title="2 Minutes Later" guid="plex://movie/5d776845999c64001ec3234c" parentTitle="" instanceRatingKey="" index="1" originallyAvailableAt="2007-04-13" addedAt="1215276889" refreshedAt="1659144719">
<MediaItem id="9321">
<MediaPart id="152394" file="/share/CACHEDEV1_DATA/video/movies/2 Minutes Later (2007) {imdb-tt0892033}/2 Minutes Later {imdb-tt0892033}.avi" size="734126080" openSubtitleHash="984c8c89827d1183" hash="78f6d4b11c856296ddf624b1eee47fe71110dfd7" duration="4094136">
<MediaStream id="419459" index="0" type="1" codec="mpeg4" bitDepth="8" chromaLocation="left" chromaSubsampling="4:2:0" codecID="XVID" codedHeight="352" codedWidth="624" frameRate="23.976" height="352" level="5" profile="advanced simple" refFrames="1" requiredBandwidths="2078,1622,1452,1349,1322,1322,1322,1322" width="624" />
<MediaStream id="419460" index="1" type="2" channels="2" codec="mp3" audioChannelLayout="stereo" requiredBandwidths="136,136,136,136,136,136,136,136" samplingRate="48000" streamIdentifier="1" />
</MediaPart>
</MediaItem>
</MetadataItem>
</MediaContainer>

```

Tested with both movies and episodes

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [November 6, 2022, 11:49pm UTC](https://forums.plex.tv/t/what-algorithm-does-plex-use-to-get-database-hash-value/803621/14 "2022-11-06T23:49:17Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
