@@ -22,44 +22,40 @@ function nextBestFormat(formats, isLive) {
22
22
return formats . find ( format => ! format . bitrate ) || formats [ 0 ] ;
23
23
}
24
24
25
- function download ( url , options = { } ) {
26
- return new Promise ( ( resolve , reject ) => {
27
- ytdl . getInfo ( url , ( err , info ) => {
28
- if ( err ) return reject ( err ) ;
29
- // Prefer opus
30
- const format = info . formats . find ( filter ) ;
31
- const canDemux = format && info . length_seconds != 0 ;
32
- if ( canDemux ) options = { ...options , filter } ;
33
- else if ( info . length_seconds != 0 ) options = { ...options , filter : 'audioonly' } ;
34
- if ( canDemux ) {
35
- const demuxer = new prism . opus . WebmDemuxer ( ) ;
36
- return resolve ( ytdl . downloadFromInfo ( info , options ) . pipe ( demuxer ) . on ( 'end' , ( ) => demuxer . destroy ( ) ) ) ;
37
- } else {
38
- const bestFormat = nextBestFormat ( info . formats , info . player_response . videoDetails . isLiveContent ) ;
39
- if ( ! bestFormat ) return reject ( 'No suitable format found' ) ;
40
- const transcoder = new prism . FFmpeg ( {
41
- args : [
42
- '-reconnect' , '1' ,
43
- '-reconnect_streamed' , '1' ,
44
- '-reconnect_delay_max' , '5' ,
45
- '-i' , bestFormat . url ,
46
- '-analyzeduration' , '0' ,
47
- '-loglevel' , '0' ,
48
- '-f' , 's16le' ,
49
- '-ar' , '48000' ,
50
- '-ac' , '2' ,
51
- ] ,
52
- } ) ;
53
- const opus = new prism . opus . Encoder ( { rate : 48000 , channels : 2 , frameSize : 960 } ) ;
54
- const stream = transcoder . pipe ( opus ) ;
55
- stream . on ( 'close' , ( ) => {
56
- transcoder . destroy ( ) ;
57
- opus . destroy ( ) ;
58
- } ) ;
59
- return resolve ( stream ) ;
60
- }
25
+ async function download ( url , options = { } ) {
26
+ const info = await ytdl . getInfo ( url ) ;
27
+ // Prefer opus
28
+ const format = info . formats . find ( filter ) ;
29
+ const canDemux = format && info . length_seconds != 0 ;
30
+ if ( canDemux ) options = { ...options , filter } ;
31
+ else if ( info . length_seconds != 0 ) options = { ...options , filter : 'audioonly' } ;
32
+ if ( canDemux ) {
33
+ const demuxer = new prism . opus . WebmDemuxer ( ) ;
34
+ return ytdl . downloadFromInfo ( info , options ) . pipe ( demuxer ) . on ( 'end' , ( ) => demuxer . destroy ( ) ) ;
35
+ } else {
36
+ const bestFormat = nextBestFormat ( info . formats , info . player_response . videoDetails . isLiveContent ) ;
37
+ if ( ! bestFormat ) throw new Error ( 'No suitable format found' ) ;
38
+ const transcoder = new prism . FFmpeg ( {
39
+ args : [
40
+ '-reconnect' , '1' ,
41
+ '-reconnect_streamed' , '1' ,
42
+ '-reconnect_delay_max' , '5' ,
43
+ '-i' , bestFormat . url ,
44
+ '-analyzeduration' , '0' ,
45
+ '-loglevel' , '0' ,
46
+ '-f' , 's16le' ,
47
+ '-ar' , '48000' ,
48
+ '-ac' , '2' ,
49
+ ] ,
61
50
} ) ;
62
- } ) ;
51
+ const opus = new prism . opus . Encoder ( { rate : 48000 , channels : 2 , frameSize : 960 } ) ;
52
+ const stream = transcoder . pipe ( opus ) ;
53
+ stream . on ( 'close' , ( ) => {
54
+ transcoder . destroy ( ) ;
55
+ opus . destroy ( ) ;
56
+ } ) ;
57
+ return stream ;
58
+ }
63
59
}
64
60
65
61
module . exports = Object . assign ( download , ytdl ) ;
0 commit comments