Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create guide for seeking audio resource #1483

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d144be2
Create guide for seeking audio resource
Fyphen1223 Jun 16, 2023
19d5674
Rename seeking to seeking.md
Fyphen1223 Jun 16, 2023
5f74157
Update sidebar.ts
Fyphen1223 Jun 16, 2023
b45a3b7
Update seeking.md
Fyphen1223 Jun 16, 2023
2e18256
Update seeking.md
Fyphen1223 Jun 16, 2023
f756261
Update seeking.md
Fyphen1223 Jun 16, 2023
03f6a16
Update seeking.md
Fyphen1223 Jun 16, 2023
7e0d8df
Update seeking.md
Fyphen1223 Jun 16, 2023
eb7eeb8
Update seeking.md
Fyphen1223 Jun 16, 2023
2c5a88f
Update seeking.md
Fyphen1223 Jun 16, 2023
40d0feb
Update seeking.md
Fyphen1223 Jun 16, 2023
617f045
Update seeking.md
Fyphen1223 Jun 16, 2023
bad953e
Fixed miss
Fyphen1223 Jun 27, 2023
ed8fc75
Fixed typo
Fyphen1223 Jun 27, 2023
22e99a4
Fixed typo, clean up
Fyphen1223 Jun 27, 2023
d7002c8
Fixed typo
Fyphen1223 Jun 28, 2023
2c8442c
Fixed typo
Fyphen1223 Jun 28, 2023
0bd1144
Update seeking.md
Fyphen1223 Jul 11, 2023
ca7e0f4
Update seeking.md
Fyphen1223 Jul 17, 2023
a9949db
Update seeking.md
Fyphen1223 Jul 17, 2023
85cbbeb
Update seeking.md
Fyphen1223 Jul 17, 2023
ea10ddb
Update seeking.md
Fyphen1223 Jul 17, 2023
636f6a2
Merge branch 'main' into main
Fyphen1223 Aug 1, 2023
ad259d2
Updated for ESLint
Fyphen1223 Aug 1, 2023
e80ba42
Update guide/voice/seeking.md
Fyphen1223 Aug 1, 2023
370cee2
Update guide/voice/seeking.md
Fyphen1223 Aug 1, 2023
cf704f5
Update guide/voice/seeking.md
Fyphen1223 Aug 1, 2023
3a0f0ff
Update guide/voice/seeking.md
Fyphen1223 Aug 1, 2023
6cb224a
Update guide/voice/seeking.md
Fyphen1223 Aug 1, 2023
06a8f86
Update seeking.md
Fyphen1223 Aug 1, 2023
b378a52
Update guide/voice/seeking.md
Fyphen1223 Aug 1, 2023
b4ce221
Update seeking.md
Fyphen1223 Aug 1, 2023
9d91bdf
Update seeking.md
Fyphen1223 Aug 1, 2023
4d148f1
Update seeking.md
Fyphen1223 Aug 1, 2023
cf66d0c
Updated for ESLint
Fyphen1223 Aug 24, 2023
42f89d3
Update for just ESLint
Fyphen1223 Aug 25, 2023
ce1f35d
Merge branch 'main' into main
Fyphen1223 Aug 25, 2023
c2cfb98
Merge branch 'discordjs:main' into main
Fyphen1223 Oct 14, 2023
80d1dc3
Merge branch 'main' into main
Fyphen1223 Oct 16, 2023
b153ba4
Merge branch 'main' into main
Fyphen1223 Nov 6, 2023
f0a81c5
Merge branch 'main' into main
Fyphen1223 Nov 14, 2023
1b4d980
Merge branch 'main' into main
Fyphen1223 Dec 12, 2023
3ab0573
Merge branch 'main' into main
Fyphen1223 Dec 18, 2023
15454da
Merge branch 'main' into main
Fyphen1223 Jan 15, 2024
5a0c84e
Merge branch 'main' into main
Fyphen1223 May 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions guide/.vuepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default {
'/voice/voice-connections.md',
'/voice/audio-player.md',
'/voice/audio-resources.md',
'/voice/seeking.md'
],
},
],
Expand Down
44 changes: 44 additions & 0 deletions guide/voice/seeking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Seek

discord.js does not have a built-in method for seeking audio resources. However external libraries such as `prism-media` can be used to tackle this issue.

### Sample code

To seek resource, you can create a new function with the following code:

```js
// Require necessary package
const prism = require('prism-media');

function createFFmpegStream(stream, seek) {
let seekPosition = '0';
if (seek) seekPosition = String(seek);
Fyphen1223 marked this conversation as resolved.
Show resolved Hide resolved
const transcoder = new prism.FFmpeg({
args: ['-analyzeduration', '0', '-loglevel', '0', '-f', 's16le', '-ar', '48000', '-ac', '2', '-ss', seekPosition, '-ab', '320',],

Check warning on line 17 in guide/voice/seeking.md

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected trailing comma

Check warning on line 17 in guide/voice/seeking.md

View workflow job for this annotation

GitHub Actions / ESLint

A space is required after ','
});
const s16le = stream.pipe(transcoder);
const opus = s16le.pipe(new prism.opus.Encoder({ rate: 48000, channels: 2, frameSize: 960 }));
return opus;
}
```
The first argument for this function should be audio stream, and second one should be int within duration of the stream.
The function returns the seeked stream. For more configuration options you can look at the [prism media documentation](https://amishshah.github.io/prism-media/).

### Complete Code

```js
const { createAudioResource, createAudioPlayer } = require('@discordjs/voice');
const fs = require('fs');

const player = createAudioPlayer();
const normalAudioResource = createAudioResource('Your audio file path');

player.play(normalAudioResource);

const seekedAudioStream = createFFmpegStream(fs.createReadStream('Your audio file path'), 10); // Seek to 10s
const seekedAudioResource = createAudioResource(seekedAudioStream);

player.play(seekedAudioResource);
```

The first argument of the `createFFmpegStream` method would be your stream.
Loading