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.

178 lines
5.4 KiB

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