Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
4.0 KiB

  1. Two years ago, I created a video streaming server from scratch in Node; looking back, I realized that I must have had way too much free time.
  2. I wanted something that I could use to embed videos in websites and aggregate all of my public videos.
  3. In the end, I created a lightweight node application with an administration interface, API tokens, and the ability to stream videos.
  4. Now, I can embed videos in my blog like this:
  5. <customHTML />
  6. Although I could have accomplished the same thing with zero effort by using Youtube-- that's now how I roll.
  7. I wanted to get better at programming with Node, and I wanted a self-hostable lightweight open-source project.
  8. Let's take a quick look at the website I ended up building:
  9. ![](media/diy-video-hosting/video3.PNG)
  10. The videos project's main page is a gallery that displays all of the videos on the same page with a search bar at the top for filtering.
  11. What is notable about this page is that all of the icons displayed are gifs generated by the backend using [ffmpeg](https://ffmpeg.org/).
  12. After you click on a video, it takes you to a separate page to watch the video or download it.
  13. Additionally, the video page lets you copy a streamable link to the video so you can embed it in a website or open it in VLC.
  14. The next notable feature of the website is the user management and privilege aspect of the system.
  15. Regular users can log into the website and watch non-listed videos.
  16. Additionally, administrators can manage other users and generate API tokens.
  17. API tokens get used to generate video share links to watch a non-listed video without log-into the website.
  18. ![](media/diy-video-hosting/video2.PNG)
  19. There is also a page listed "System Controls."
  20. The system page enables admin users to update some environment variables like where the videos are located and the server's URL.
  21. The server URL gets used when generating share links for end-users to copy and share.
  22. The re-index button tells the system to scan for new videos and generate icons.
  23. ![](media/diy-video-hosting/video1.PNG)
  24. Being able to download a full video is straightforward since you just serve the entire file.
  25. However, to stream a video, you have to send it to the client chunk by chunk, and which chunk gets downloaded gets determined by the end client-- enabling things like buffering and skipping forward.
  26. The following code snippet is the core node code that I ended up using to provide this functionality.
  27. The client sends a byte array indicating the range in which they want to download.
  28. The server then responds by reading that portion of the video file and sending it to the client.
  29. ```javascript
  30. const parts = request.headers.range.replace(/bytes=/, "").split("-");
  31. const start = parseInt(parts[0], 10);
  32. const end = parts[1]
  33. ? parseInt(parts[1], 10)
  34. : fileSize-1;
  35. const chunksize = (end-start)+1;
  36. const file = fs.createReadStream(path, {start, end});
  37. const head =
  38. {
  39. 'Content-Range': `bytes ${start}-${end}/${fileSize}`,
  40. 'Accept-Ranges': 'bytes',
  41. 'Content-Length': chunksize,
  42. 'Content-Type': 'video/mp4',
  43. };
  44. result.writeHead(206, head);
  45. file.pipe(result);
  46. ```
  47. Another fun tidbit from this project is that I just created a Docker script to run the project.
  48. The Docker container gets derived from a generic node container, and then I installed the ffmpeg and gifski packages-- enabling me to generate the video icons.
  49. ```bash
  50. FROM node:buster-slim
  51. COPY package.json package.json
  52. RUN apt-get update && \
  53. apt-get install ffmpeg -y && \
  54. apt-get install wget -y && \
  55. cd /root && \
  56. wget https://github.com/jrtechs/static-storage/raw/master/gifski.deb && \
  57. dpkg -i /root/gifski.deb && \
  58. rm /root/gifski.deb
  59. RUN npm install
  60. EXPOSE 4000
  61. WORKDIR /src/
  62. CMD npm start
  63. ```
  64. Although I don't use this project a lot, I maintain it since it is effortless to host and allows me to share videos without using a centralized service like YouTube. If you are interested in the project, check it out on [Github](https://github.com/jrtechs/HomeBrewPlex) and contribute by submitting issues and creating pull requests.