Create torrent with files from different directories from CLI

bittorrentcommand line

I have a directory structure that looks like:

dirA
   fileA1
   fileA2
   ...
dirB
   fileB1
   fileB2
   ...

I would like to create a torrent using CLI utilities that contains:

dirA/fileA1
dirB/fileB1

(Note: this is a simplified example. In reality, there are four directories and thousands of files in each, and I would like to select ~100 files out of each directory. So solutions that involve simply excluding specific files won't work.)

So far I have tried:

  • ctorrent only lets you specify a single file or directory
  • mktorrent only lets you specify a single file or directory
  • transmission-create only lets you specify a single file or directory
  • py3torrentcreator only lets you specify a single file or directory. It does allow you to specify a pattern of files to exclude, but there are way too many other files to exclude them individually.

I also tried using the Python bindings for libtorrent, but their add_files method strips out the directory names:

>>> import libtorrent as lt
>>> fs = lt.file_storage()
>>> lt.add_files(fs, 'dirA/fileA1')
>>> lt.add_files(fs, 'dirB/fileB1')
>>> print fs.at(0).path
fileA1
>>> t = lt.create_torrent(fs)
>>> lt.set_piece_hashes(t, '.')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: No such file or directory

Is there any way to accomplish this?

Best Answer

The easiest way to do this, that I know of, is to create a single directory containing symlinks to the different files or directories you would like to add to the torrent.

Add symlinks to a parent directory
cd ~/Shared/parent-dir/
ln -s /path/to/file
ln -s /path/to/dir

Create your torrent
Testing with transmission-create, you can create a new torrent using this source folder and each symlink will be traversed.

transmission-create ~/Shared/parent-dir/

There is no way to store the full filepath in a torrent's meta info for files that are not descendants of parent-dir. When a peer downloads the multi-file torrent, a directory is created using name of the torrent that is found in its meta info. This directory is used as the top most parent directory for all files included in the meta info.

Here is the output of the meta info for a torrent I have called bt-symlinks.torrent. Notice how only paths to files are stored in the meta info and they always begin with the name(infile)1 used as their top most2 directory3.

transmission-show bt-symlinks.torrent

Name: bt-symlinks
File: .torrent

GENERAL

  Name: bt-symlinks
  Hash: 35af9b734284f9259763defe6095424fe3b79b42
  Created by: Transmission/2.82 (14160)
  Created on: Sat Dec 27 12:04:59 2014
  Piece Count: 2357
  Piece Size: 64.00 KiB
  Total Size: 154.4 MB
  Privacy: Public torrent

TRACKERS

FILES

  bt-symlinks/bt-symlinks.torrent (57.40 kB)
  bt-symlinks/gifs/Bill-Cosby-Jell-o-GIF.gif (810.3 kB)
  bt-symlinks/gifs/Firefly_Lantern_Animation_by_ProdigyBombay.gif (485.2 kB)
  bt-symlinks/gifs/L-cake.gif (455.2 kB)
  bt-symlinks/gifs/L-sweets.gif (871.0 kB)
  bt-symlinks/gifs/Metroid (NES) Music - Kraids Hideout.mp4 (4.16 MB)
  bt-symlinks/gifs/Phantasy Star II_Mother Brain.gif (530.5 kB)
Related Question