Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to download the file range pieces? #206

Open
pangoo-reuse opened this issue Apr 20, 2022 · 2 comments
Open

how to download the file range pieces? #206

pangoo-reuse opened this issue Apr 20, 2022 · 2 comments

Comments

@pangoo-reuse
Copy link

like .torrent contains 2 files , file1,file2.
file1 is a video file.
for example ,file1 has 60 pieces , i want to play file1 when download,
i want to play third seconds first,or fast jump 5th seconds ,
i want bt downloader download 5th seconds (like 5 -10 pieces) first,so what shoud i do ?thx a lot

@jokechen102
Copy link

Did your question solved? I have the same question ,too.

@pvishnyakov
Copy link

pvishnyakov commented Jul 11, 2023

Implement FilePrioritySkipSelector to select the file and PieceSelector to tell BtClient what pieces you need, several hints:

	private final FilePrioritySkipSelector filePrioritySkipSelector =
			torrentFile ->
					torrent.getFiles().indexOf(torrentFile)==selectedFileIndex
							? FilePriority.HIGH_PRIORITY
							: FilePriority.SKIP;
	private final PieceSelector pieceSelector = new PieceSelector() {
		@Override
		public IntStream getNextPieces(BitSet bitSet, PieceStatistics pieceStatistics) {
			return IntStream.of(piecesToDownload);
		}
	};
	private boolean piecesDownloaded() {
		for (int i:piecesToDownload) if (!pieceMap[i]) return false;
		return true;
	}
	private BtClient createClient(URI uri) {
		try {
			return Bt.client(runtime)
					.storage(storage)
					.torrent(uri.toURL())
					.afterTorrentFetched(tf -> {
...
						pieceMap = new boolean[piecesInTorrent];
					        piecesToDownload = IntStream.concat(IntStream.range(firstFilePiece, firstFilePiece+piecesToPrepare), IntStream.rangeClosed(lastFilePiece-criticalEndPieces+1, lastFilePiece)).toArray();
						runtime.getEventSource().onPieceVerified(torrent.getTorrentId(), event -> {
							pieceMap[event.getPieceIndex()]=true;
							if (piecesDownloaded()) {
                                                             // update piecesToDownload array to supply next portion of pieces
							}
						});
					})
					.fileSelector(filePrioritySkipSelector)
					.selector(pieceSelector)
					.stopWhenDownloaded()
					.build();
		} catch (Exception e) {
			log("Error creating BT client", e);
			return null;
		}
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants