Skip to content

Commit

Permalink
added current time to media player
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowLp174 committed Nov 11, 2023
1 parent 556da35 commit 8954bc4
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Media.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class Media {
* @class
* @augments Media
* @description An advanced version of the Media class. It also includes media controls like pausing.
*
* @property {number} seconds - The amount of seconds passed during playback. Extracted from ffmpeg
* @property {string} timestamp - The current timestamp as given by ffmpeg. "hh:mm:ss"
*/
class MediaPlayer extends Media {
/**
Expand Down Expand Up @@ -152,6 +155,9 @@ class MediaPlayer extends Media {
this.ready = true;
this.volCache = null;

this.seconds = 0;
this.currTimestamp = "00:00:00";

this.volumeTransformer = new prism.VolumeTransformer({ type: "s16le", volume: 1 });
this.volumeTransformer.pipe(this.ffmpeg.stdin);

Expand Down Expand Up @@ -437,6 +443,9 @@ class MediaPlayer extends Media {
this.currTime = "00:00:00";
}
#setupFmpeg() {
this.seconds = 0;
this.currTimestamp = "00:00:00";

this.fpcm.on("exit", async (_c, s) => {
if (s == "SIGTERM" || this.ffmpegKilled) return this.ffmpegKilled = false; // killed intentionally
this.#ffmpegFinished();
Expand All @@ -446,7 +455,14 @@ class MediaPlayer extends Media {
console.log("Ffmpeg error: ", e);
});
this.ffmpeg.stderr.on("data", (chunk) => {
if (this.logs) console.log("err", Buffer.from(chunk).toString());
const output = Buffer.from(chunk).toString();
if (this.logs) console.log("err", output);

if (!output.includes("time=")) return;
var time = output.slice(output.indexOf("time=") + "time=".length, output.indexOf("time=") + "time=".length + 11)
this.currTimestamp = time;
time = MediaPlayer.timestampToSeconds(time);
this.seconds = time;
});
this.ffmpeg.stdout.on("data", (chunk) => {
if (this.logs) console.log("OUT", Buffer.from(chunk.toString()));
Expand Down

0 comments on commit 8954bc4

Please sign in to comment.