-
Notifications
You must be signed in to change notification settings - Fork 0
Retrieve information from `.torrent` file
JackyHe398 on OldHome_main edited this page Aug 1, 2026
·
2 revisions
When you want to inspect or modify .torrent metainfo directly, use TorrentMetaInfo.
Torrent is for runtime tracker and peer state, while TorrentMetaInfo handles the
metainfo structure itself.
Load metainfo directly from a .torrent file:
from torrentlib import TorrentMetaInfo
metainfo = TorrentMetaInfo.from_file("example.torrent")
print(metainfo.info_hash)
print(metainfo.name)
print(metainfo.total_size)
print(metainfo.piece_length)
print(metainfo.has_info)
print(metainfo.info_bytes)Create a Torrent from that metainfo when you want tracker or peer operations:
from torrentlib import Torrent, TorrentMetaInfo
metainfo = TorrentMetaInfo.from_file("example.torrent")
torrent = Torrent.from_metainfo(metainfo)You can modify metainfo directly through TorrentMetaInfo.data and TorrentMetaInfo.info.
Top-level metainfo fields such as announce and announce-list live in data, while
fields inside the torrent info dictionary live in info.
from torrentlib import TorrentMetaInfo
metainfo = TorrentMetaInfo.from_file("example.torrent")
# Modify top-level metainfo fields.
metainfo.data[b"announce"] = b"https://tracker.example.com/announce"
metainfo.data[b"announce-list"] = [
[b"https://tracker.example.com/announce"],
[b"udp://tracker2.example.com:6969/announce"],
]
# Modify fields inside the info dictionary.
if metainfo.info is not None:
metainfo.info[b"private"] = 1
# Recompute derived state such as info_hash when info changed.
metainfo.refresh()
# Export the updated metainfo as bytes or as a .torrent file.
encoded = metainfo.to_bytes()
metainfo.to_file("example-private.torrent")Custom tags are also supported in both places:
from torrentlib import TorrentMetaInfo
metainfo = TorrentMetaInfo.from_file("example.torrent")
metainfo.data[b"x-meta-tag"] = b"custom value"
if metainfo.info is not None:
metainfo.info[b"x-info-tag"] = b"custom value"
metainfo.refresh()