Lightweight node app to use in place of plex to self host your video and movie collections. Running on just under 50MB of ram this is ideal for people looking to host videos on minimal hardware.
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.

175 lines
5.2 KiB

  1. const recursive = require('./recursiveTraversal');
  2. const generatePreview = require('ffmpeg-generate-video-preview')
  3. const fs = require('fs');
  4. var privateVideos = null;
  5. var publicVideos = null;
  6. function createIndex(filename, videos, templateKey)
  7. {
  8. return new Promise(function(resolve, reject)
  9. {
  10. console.log("Generating icon for " + filename);
  11. var splitArray = filename.split('/');
  12. var name = splitArray[splitArray.length -1];
  13. const icon = 'icon/' + templateKey + '/' + name + ".gif";
  14. if (!fs.existsSync(icon))
  15. {
  16. var options =
  17. {
  18. input: filename,
  19. output: icon,
  20. width: 500,
  21. numFrames: 60
  22. };
  23. console.log(options);
  24. generatePreview(options).then(function(metaData)
  25. {
  26. console.log(metaData);
  27. resolve();
  28. }).catch(function(error)
  29. {
  30. resolve();
  31. })
  32. }
  33. else
  34. {
  35. resolve();
  36. }
  37. })
  38. }
  39. async function runTasksSync(files, videos, templateKey)
  40. {
  41. for(var file of files)
  42. {
  43. await createIndex(file, videos, templateKey);
  44. }
  45. }
  46. module.exports =
  47. {
  48. indexVideos: function(rootDir, videos, templateKey)
  49. {
  50. return new Promise(function(resolve, reject)
  51. {
  52. recursive(rootDir, function (err, files)
  53. {
  54. if(files !== undefined)
  55. {
  56. files.forEach(file =>
  57. {
  58. videos.push({name: file.replace(rootDir, '')});
  59. });
  60. runTasksSync(files.splice(0, files.length/2), videos, templateKey);
  61. runTasksSync(files.splice(files.length/2, files.length), videos, templateKey);
  62. resolve();
  63. }
  64. else
  65. {
  66. resolve();
  67. }
  68. });
  69. }).catch(function(error)
  70. {
  71. //console.log(error);
  72. })
  73. },
  74. getVideosForTemplate: function(templateContext, templateKey)
  75. {
  76. return new Promise(function(resolve, reject)
  77. {
  78. var videos, rootDir;
  79. if(templateKey === "public")
  80. {
  81. videos = publicVideos;
  82. rootDir = process.env.PUBLIC_DIR
  83. }
  84. else
  85. {
  86. videos = privateVideos;
  87. rootDir = process.env.PRIVATE_DIR
  88. }
  89. if(videos === null)
  90. {
  91. videos = [];
  92. module.exports.indexVideos(rootDir, videos, templateKey)
  93. .then(function()
  94. {
  95. templateContext[templateKey] = videos;
  96. if(templateKey === "public")
  97. publicVideos = videos;
  98. else
  99. privateVideos = videos;
  100. resolve();
  101. })
  102. }
  103. else
  104. {
  105. templateContext[templateKey] = videos;
  106. resolve();
  107. }
  108. })
  109. },
  110. isPublicVideo: function(videoName)
  111. {
  112. if(publicVideos == null)
  113. {
  114. publicVideos = [];
  115. module.exports.indexVideos(process.env.PUBLIC_DIR, publicVideos, "public").then(function()
  116. {
  117. for(var i = 0; i < publicVideos.length; i++)
  118. {
  119. const splitArray = publicVideos[i].name.split('/');
  120. const name = splitArray[splitArray.length -1];
  121. videoName = videoName.split('/').join("");
  122. if(name === videoName)
  123. {
  124. return true;
  125. }
  126. }
  127. return false;
  128. });
  129. }
  130. else
  131. {
  132. for(var i = 0; i < publicVideos.length; i++)
  133. {
  134. const splitArray = publicVideos[i].name.split('/');
  135. const name = splitArray[splitArray.length -1];
  136. videoName = videoName.split('/').join("");
  137. if(name + "" === videoName)
  138. {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. },
  145. reIndexVideos: function()
  146. {
  147. publicVideos = [];
  148. privateVideos = [];
  149. module.exports.indexVideos(process.env.PUBLIC_DIR, publicVideos, "public");
  150. module.exports.indexVideos(process.env.PRIVATE_DIR, privateVideos, "private");
  151. },
  152. getPublicVideoCount: function()
  153. {
  154. return (publicVideos === null) ? 0: publicVideos.length;
  155. },
  156. getPrivateVideoCount: function()
  157. {
  158. return (privateVideos === null) ? 0: privateVideos.length;
  159. }
  160. };