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.

168 lines
5.1 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. })
  30. }
  31. else
  32. {
  33. resolve();
  34. }
  35. })
  36. }
  37. async function runTasksSync(files, videos, templateKey)
  38. {
  39. for(var file of files)
  40. {
  41. await createIndex(file, videos, templateKey);
  42. }
  43. }
  44. module.exports =
  45. {
  46. indexVideos: function(rootDir, videos, templateKey)
  47. {
  48. return new Promise(function(resolve, reject)
  49. {
  50. recursive(rootDir, function (err, files)
  51. {
  52. files.forEach(file =>
  53. {
  54. videos.push({name: file.replace(rootDir, '')});
  55. });
  56. runTasksSync(files.splice(0, files.length/2), videos, templateKey);
  57. runTasksSync(files.splice(files.length/2, files.length), videos, templateKey);
  58. resolve();
  59. });
  60. }).catch(function(error)
  61. {
  62. //console.log(error);
  63. })
  64. },
  65. getVideosForTemplate: function(templateContext, templateKey)
  66. {
  67. return new Promise(function(resolve, reject)
  68. {
  69. var videos, rootDir;
  70. if(templateKey === "public")
  71. {
  72. videos = publicVideos;
  73. rootDir = configManager.getPublicDirectory();
  74. }
  75. else
  76. {
  77. videos = privateVideos;
  78. rootDir = configManager.getRootDirectory();
  79. }
  80. if(videos === null)
  81. {
  82. videos = [];
  83. module.exports.indexVideos(rootDir, videos, templateKey)
  84. .then(function()
  85. {
  86. templateContext[templateKey] = videos;
  87. if(templateKey === "public")
  88. publicVideos = videos;
  89. else
  90. privateVideos = videos;
  91. resolve();
  92. })
  93. }
  94. else
  95. {
  96. templateContext[templateKey] = videos;
  97. resolve();
  98. }
  99. })
  100. },
  101. isPublicVideo: function(videoName)
  102. {
  103. if(publicVideos == null)
  104. {
  105. publicVideos = [];
  106. rootDir = configManager.getPublicDirectory();
  107. module.exports.indexVideos(rootDir, publicVideos, "public").then(function()
  108. {
  109. for(var i = 0; i < publicVideos.length; i++)
  110. {
  111. const splitArray = publicVideos[i].name.split('/');
  112. const name = splitArray[splitArray.length -1];
  113. videoName = videoName.split('/').join("");
  114. if(name === videoName)
  115. {
  116. return true;
  117. }
  118. }
  119. return false;
  120. });
  121. }
  122. else
  123. {
  124. for(var i = 0; i < publicVideos.length; i++)
  125. {
  126. const splitArray = publicVideos[i].name.split('/');
  127. const name = splitArray[splitArray.length -1];
  128. videoName = videoName.split('/').join("");
  129. if(name + "" === videoName)
  130. {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. },
  137. reIndexVideos: function()
  138. {
  139. publicVideos = [];
  140. privateVideos = [];
  141. module.exports.indexVideos(configManager.getPublicDirectory(), publicVideos, "public");
  142. module.exports.indexVideos(configManager.getRootDirectory(), privateVideos, "private");
  143. },
  144. getPublicVideoCount: function()
  145. {
  146. return (publicVideos === null) ? 0: publicVideos.length;
  147. },
  148. getPrivateVideoCount: function()
  149. {
  150. return (privateVideos === null) ? 0: privateVideos.length;
  151. }
  152. };