Turn audio into a shareable video. forked from nypublicradio/audiogram

index.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. var path = require("path"),
  2. queue = require("d3").queue,
  3. mkdirp = require("mkdirp"),
  4. rimraf = require("rimraf"),
  5. serverSettings = require("../settings/"),
  6. transports = require("../lib/transports/"),
  7. logger = require("../lib/logger/"),
  8. getDuration = require("./duration.js"),
  9. getWaveform = require("./waveform.js"),
  10. initializeCanvas = require("./initialize-canvas.js"),
  11. drawFrames = require("./draw-frames.js"),
  12. combineFrames = require("./combine-frames.js"),
  13. trimAudio = require("./trim.js");
  14. function Audiogram(id) {
  15. // Unique audiogram ID
  16. this.id = id;
  17. // File locations to use
  18. this.dir = path.join(serverSettings.workingDirectory, this.id);
  19. this.audioPath = path.join(this.dir, "audio");
  20. this.videoPath = path.join(this.dir, "video.mp4");
  21. this.frameDir = path.join(this.dir, "frames");
  22. return this;
  23. }
  24. // Probe an audio file for its duration, compute the number of frames required
  25. Audiogram.prototype.getDuration = function(cb) {
  26. var self = this;
  27. this.status("duration");
  28. getDuration(this.audioPath, function(err, duration){
  29. if (err) {
  30. return cb(err);
  31. }
  32. if (self.settings.theme.maxDuration && self.settings.theme.maxDuration < duration) {
  33. cb("Exceeds max duration of " + self.settings.theme.maxDuration + "s");
  34. }
  35. self.set("numFrames", self.numFrames = Math.floor(duration * self.settings.theme.framesPerSecond));
  36. cb(null);
  37. });
  38. };
  39. // Get the waveform data from the audio file, split into frames
  40. Audiogram.prototype.getWaveform = function(cb) {
  41. var self = this;
  42. this.status("waveform");
  43. getWaveform(this.audioPath, {
  44. numFrames: this.numFrames,
  45. samplesPerFrame: this.settings.theme.samplesPerFrame
  46. }, function(err, waveform){
  47. return cb(err, self.waveform = waveform);
  48. });
  49. };
  50. // Trim the audio by the start and end time specified
  51. Audiogram.prototype.trimAudio = function(start, end, cb) {
  52. var self = this;
  53. this.status("trim");
  54. // FFmpeg needs an extension to sniff
  55. var trimmedPath = this.audioPath + "-trimmed.mp3";
  56. trimAudio({
  57. origin: this.audioPath,
  58. destination: trimmedPath,
  59. startTime: start,
  60. endTime: end
  61. }, function(err){
  62. if (err) {
  63. return cb(err);
  64. }
  65. self.audioPath = trimmedPath;
  66. return cb(null);
  67. });
  68. };
  69. // Initialize the canvas and draw all the frames
  70. Audiogram.prototype.drawFrames = function(cb) {
  71. var self = this;
  72. this.status("renderer");
  73. initializeCanvas(this.settings.theme, function(err, renderer){
  74. if (err) {
  75. return cb(err);
  76. }
  77. self.status("frames");
  78. drawFrames(renderer, {
  79. width: self.settings.theme.width,
  80. height: self.settings.theme.height,
  81. numFrames: self.numFrames,
  82. frameDir: self.frameDir,
  83. caption: self.settings.caption,
  84. waveform: self.waveform,
  85. tick: function() {
  86. transports.incrementField(self.id, "framesComplete");
  87. }
  88. }, cb);
  89. });
  90. };
  91. // Combine the frames and audio into the final video with FFmpeg
  92. Audiogram.prototype.combineFrames = function(cb) {
  93. this.status("combine");
  94. combineFrames({
  95. framePath: path.join(this.frameDir, "%06d.png"),
  96. audioPath: this.audioPath,
  97. videoPath: this.videoPath,
  98. framesPerSecond: this.settings.theme.framesPerSecond
  99. }, cb);
  100. };
  101. // Master render function, queue up steps in order
  102. Audiogram.prototype.render = function(cb) {
  103. var self = this,
  104. q = queue(1);
  105. this.status("audio-download");
  106. // Set up tmp directory
  107. q.defer(mkdirp, this.frameDir);
  108. // Download the stored audio file
  109. q.defer(transports.downloadAudio, "audio/" + this.id, this.audioPath);
  110. // If the audio needs to be clipped, clip it first and update the path
  111. if (this.settings.start || this.settings.end) {
  112. q.defer(this.trimAudio.bind(this), this.settings.start || 0, this.settings.end);
  113. }
  114. // Get the audio's duration for computing number of frames
  115. q.defer(this.getDuration.bind(this));
  116. // Get the audio waveform data
  117. q.defer(this.getWaveform.bind(this));
  118. // Draw all the frames
  119. q.defer(this.drawFrames.bind(this));
  120. // Combine audio and frames together with ffmpeg
  121. q.defer(this.combineFrames.bind(this));
  122. // Upload video to S3 or move to local storage
  123. q.defer(transports.uploadVideo, this.videoPath, "video/" + this.id + ".mp4");
  124. // Delete working directory
  125. q.defer(rimraf, this.dir);
  126. // Final callback, results in a URL where the finished video is accessible
  127. q.await(function(err){
  128. if (!err) {
  129. self.set("url", transports.getURL(self.id));
  130. }
  131. return cb(err);
  132. });
  133. return this;
  134. };
  135. Audiogram.prototype.set = function(field, value) {
  136. logger.debug(field + "=" + value);
  137. transports.setField(this.id, field, value);
  138. return this;
  139. };
  140. // Convenience method for .set("status")
  141. Audiogram.prototype.status = function(value) {
  142. return this.set("status", value);
  143. };
  144. module.exports = Audiogram;