KeenEthics   >   Blog   >   KeenEthics Experience-Sharing: How to Create a Simple Torrent Player?
PUBLISH DATE:
Share on:

KeenEthics Experience-Sharing: How to Create a Simple Torrent Player?

Have you ever heard about PopcornTime? This app allows you to play a video from BitTorrent without downloading the whole file. Why not develop a similar thing but for audio? It is a good opportunity to practice and to learn some Node.js basics.

KeenEthics Experience-Sharing: How to Create a Simple Torrent Player?

Here, I will show only how to work with mp3, but you may easily reimplement other formats using libraries. As a source tracker, I will use rutracker. I have tried to use PirateBay, but it does not have a way to get tracks path without taking torrent info, which is useful to play a single track faster.

I use Electron since it is simpler, but you can easily take it to usual Node.js + simple static server. I have three main js scripts: rutracker search wrapper, client code, and server-side code. The code is not perfect, and it is very simplified for POC.

Rutracker.js: Rutracker Search Wrapper

There are two exported methods. The second one is used for distribution search to filter the titles that contain “mp3”. The first one is used for retrieving the list of tracks inside the distribution. Now, it merely displays the entire track path, which can be transformed to the tree view if needed. This code uses non-official rutracker API and simple page scraping, so it does not require any specific comments here.

Serv.js: Server-Side Code

I use fastify as a server framework. It exposes only one route, which is used for streaming torrent to audio tag. At the same time, it has 2 limitations.

Firstly, web-torrent does not support TCP/UDP in browser, while Node.js development does. Respectively, I can only use torrents that have seeders based on WebRTC fallback. In case I want to get magnet url from tracker and just play i, it will not work.

Secondly, I just choose a simpler way to pipe Node.js readable Stream to browser audio. I have found an alternative way, but it does not seem to support track seeking.

Let’s see what the code does.

I select a track on the client side and pass trackPath argument to the server. It contains the full path to a specific track including the directory structure.

Then, I get torrent info by magnet, find a specific file inside, take read stream, and pipe it to response. This url is used as file inside audio tag src. A good thing is that createReadStream supports ranges, so it is easy to implement audio seeking.

Main.js: Client Code

This file can be used for a simple search and audio output.

To Wrap Up

I have provided an example of how easy it can be to create a torrent player of some kind. Sure, it has some issues, such as the search sometimes does not work or huge files are opened slowly. Yet, it is just a POC, and most of these problems can be solved.

You can find code here: https://github.com/demogoran/ElectroPlay

Do you have an idea for a project?

If there is something you need assistance with, we will be happy to share our experience or to help you implement the idea!

Rate this article!

  • rate
  • rate
  • rate
  • rate
  • rate
5/5 Reviews: 1
You have already done it before!