-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Create guide for seeking audio resource #1483
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
Open
Fyphen1223
wants to merge
46
commits into
discordjs:main
Choose a base branch
from
Fyphen1223:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
d144be2
Create guide for seeking audio resource
Fyphen1223 19d5674
Rename seeking to seeking.md
Fyphen1223 5f74157
Update sidebar.ts
Fyphen1223 b45a3b7
Update seeking.md
Fyphen1223 2e18256
Update seeking.md
Fyphen1223 f756261
Update seeking.md
Fyphen1223 03f6a16
Update seeking.md
Fyphen1223 7e0d8df
Update seeking.md
Fyphen1223 eb7eeb8
Update seeking.md
Fyphen1223 2c5a88f
Update seeking.md
Fyphen1223 40d0feb
Update seeking.md
Fyphen1223 617f045
Update seeking.md
Fyphen1223 bad953e
Fixed miss
Fyphen1223 ed8fc75
Fixed typo
Fyphen1223 22e99a4
Fixed typo, clean up
Fyphen1223 d7002c8
Fixed typo
Fyphen1223 2c8442c
Fixed typo
Fyphen1223 0bd1144
Update seeking.md
Fyphen1223 ca7e0f4
Update seeking.md
Fyphen1223 a9949db
Update seeking.md
Fyphen1223 85cbbeb
Update seeking.md
Fyphen1223 ea10ddb
Update seeking.md
Fyphen1223 636f6a2
Merge branch 'main' into main
Fyphen1223 ad259d2
Updated for ESLint
Fyphen1223 e80ba42
Update guide/voice/seeking.md
Fyphen1223 370cee2
Update guide/voice/seeking.md
Fyphen1223 cf704f5
Update guide/voice/seeking.md
Fyphen1223 3a0f0ff
Update guide/voice/seeking.md
Fyphen1223 6cb224a
Update guide/voice/seeking.md
Fyphen1223 06a8f86
Update seeking.md
Fyphen1223 b378a52
Update guide/voice/seeking.md
Fyphen1223 b4ce221
Update seeking.md
Fyphen1223 9d91bdf
Update seeking.md
Fyphen1223 4d148f1
Update seeking.md
Fyphen1223 cf66d0c
Updated for ESLint
Fyphen1223 42f89d3
Update for just ESLint
Fyphen1223 ce1f35d
Merge branch 'main' into main
Fyphen1223 c2cfb98
Merge branch 'discordjs:main' into main
Fyphen1223 80d1dc3
Merge branch 'main' into main
Fyphen1223 b153ba4
Merge branch 'main' into main
Fyphen1223 f0a81c5
Merge branch 'main' into main
Fyphen1223 1b4d980
Merge branch 'main' into main
Fyphen1223 3ab0573
Merge branch 'main' into main
Fyphen1223 15454da
Merge branch 'main' into main
Fyphen1223 5a0c84e
Merge branch 'main' into main
Fyphen1223 7bd69a1
Merge branch 'main' into main
Fyphen1223 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
const transcoder = new prism.FFmpeg({ | ||
args: ['-analyzeduration', '0', '-loglevel', '0', '-f', 's16le', '-ar', '48000', '-ac', '2', '-ss', seekPosition, '-ab', '320',], | ||
}); | ||
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.