1
+ const Mode = require ( './Mode' )
2
+ const fs = require ( 'fs' )
3
+ const youtubedl = require ( 'youtube-dl' )
4
+ const ffmpeg = require ( 'fluent-ffmpeg' )
5
+ const { exec } = require ( 'child_process' )
6
+ const config = require ( '../config/config.js' )
7
+ const leds = require ( 'rpi-ws281x-native' )
8
+ const chalk = require ( 'chalk' )
9
+
10
+ function rgbToInt ( r , g , b ) {
11
+ return r << 16 | g << 8 | b
12
+ }
13
+
14
+ module . exports = class YoutubeMode extends Mode {
15
+ constructor ( { url } ) {
16
+ super ( )
17
+ console . log ( 'Initializing YoutubeMode' )
18
+
19
+ console . log ( 'Loading url' , url )
20
+ let video = youtubedl ( url , [ '--format=18' ] , { cwd : __dirname } )
21
+ video . on ( 'info' , function ( info ) {
22
+ console . log ( 'Download started' )
23
+ console . log ( 'filename: ' + info . filename )
24
+ console . log ( 'size: ' + info . size )
25
+ } )
26
+ video . pipe ( fs . createWriteStream ( 'myvideo.mp4' ) )
27
+ video . on ( 'end' , function ( ) {
28
+ console . log ( 'finished downloading!' )
29
+ let ff = ffmpeg ( __dirname + '/../myvideo.mp4' ) . noAudio ( ) . outputFormat ( 'rawvideo' )
30
+ console . log ( 'file loaded' )
31
+ console . log ( 'Led Count = ' + config . ledCount )
32
+ const ffmpegCommand = 'yes | ffmpeg -i myvideo.mp4 -filter:v "scale=1650:-1, crop=w=in_w:h=1:x=0:y=in_h/2:exact=1" -vcodec rawvideo -pix_fmt rgb24 out.rgb'
33
+ exec ( ffmpegCommand , ( error , stdout , stderr ) => {
34
+ if ( error ) {
35
+ console . error ( `exec error: ${ error } ` ) ;
36
+ return ;
37
+ }
38
+ console . log ( `stdout: ${ stdout } ` ) ;
39
+ console . log ( `stderr: ${ stderr } ` ) ;
40
+ console . log ( 'ffmpeg command finished' )
41
+ // let file = fs.readFileSync(__dirname + '/../out.rgb')
42
+ // let fileArray = new Uint32Array(file)
43
+ // console.log(fileArray.length + ' <------- length')
44
+ let frameNb = 0
45
+ setInterval ( ( ) => {
46
+ let readStream = fs . createReadStream ( __dirname + '/../out.rgb' , { start : frameNb * 1650 , end : ( 3 + frameNb ) * 1650 - 1 } )
47
+ let data = ''
48
+ let testChunk
49
+ readStream . on ( 'data' , function ( chunk ) {
50
+ console . log ( 'chunk loaded' )
51
+ testChunk = new Uint32Array ( chunk )
52
+ console . log ( testChunk . length )
53
+
54
+ // console.log(testChunk)
55
+ // data += chunk
56
+ } ) . on ( 'end' , function ( ) {
57
+ console . log ( 'stream read ended' )
58
+ let frame = 0 ;
59
+ let newArray = new Uint32Array ( 1650 ) . map ( ( val , index , array ) => {
60
+ let r = testChunk [ index * 3 ]
61
+ let g = testChunk [ index * 3 + 1 ]
62
+ let b = testChunk [ index * 3 + 2 ]
63
+ // console.log(chalk.rgb(r, g, b)(index))
64
+ // if( r === 189 && g === 189 && b === 189) console.log(index)
65
+ if ( index === 1649 ) console . log ( chalk . rgb ( r , g , b ) ( index ) )
66
+ if ( index === 1650 ) console . log ( `${ r } ${ g } ${ b } ` )
67
+ if ( index === 1651 ) console . log ( `${ r } ${ g } ${ b } ` )
68
+ // if (index === 1651) console.log(chalk.rgb(r, g, b)(index))
69
+ // console.log(`${r} ${g} ${b}`)
70
+ return rgbToInt ( r , g , b )
71
+ } )
72
+ console . log ( 'Rendering Frame ' + frameNb )
73
+
74
+ leds . render ( newArray )
75
+ frameNb = frameNb + 1
76
+ // let p = newArray[newArray.length - 2]
77
+ // console.log(chalk.rgb(p.r, p.g, p.b)('youpi'))
78
+ // console.log('maimmamidaimi')
79
+ } )
80
+ } , 40 )
81
+ } )
82
+ // ffmpeg -i myVideo.mp4 -vcodec rawvideo -filter:v "crop=in_w/:1:0:0" -pix_fmt yuv420p out.rgb
83
+ // ffmpeg -i myVideo.mp4 filter:v "crop=in_w/:1:0:0" out.mp4
84
+ // ffmpeg -i myVideo.mp4 -filter:v "crop=in_w:1:0:0" out.mp4
85
+ // ffmpeg -i myVideo.mp4 -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo out.rgb
86
+ // ffmpeg -i myVideo.mp4 -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb24 out.rgb
87
+ // ffmpeg -i myVideo.mp4 -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb32 out.rgb
88
+ // ffmpeg -i myvideo.mp4 -filter:v scale=1650:-1 -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb32 out.rgb
89
+ // fmpeg -i myvideo.mp4 -filter:v "scale=1650:-1" -filter:v "crop=w=in_w:h=2:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb24 out.rgb
90
+ // yes | fmpeg -i myvideo.mp4 -filter:v "scale=1650:-1" -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb24 out.rgb
91
+ // .complexFilter([
92
+ // // Duplicate video stream 3 times into streams a, b, and c
93
+ // { filter: 'split', options: '3', outputs: ['a', 'b', 'c'] },
94
+
95
+ // // Create stream 'red' by cancelling green and blue channels from stream 'a'
96
+ // { filter: 'lutrgb', options: { g: 0, b: 0 }, inputs: 'a', outputs: 'red' },
97
+
98
+ // // Create stream 'green' by cancelling red and blue channels from stream 'b'
99
+ // { filter: 'lutrgb', options: { r: 0, b: 0 }, inputs: 'b', outputs: 'green' },
100
+
101
+ // // Create stream 'blue' by cancelling red and green channels from stream 'c'
102
+ // { filter: 'lutrgb', options: { r: 0, g: 0 }, inputs: 'c', outputs: 'blue' },
103
+
104
+ // // Pad stream 'red' to 3x width, keeping the video on the left, and name output 'padded'
105
+ // { filter: 'pad', options: { w: 'iw3', h: 'ih' }, inputs: 'red', outputs: 'padded' },
106
+
107
+ // // Overlay 'green' onto 'padded', moving it to the center, and name output 'redgreen'
108
+ // { filter: 'overlay', options: { x: 'w', y: 0 }, inputs: ['padded', 'green'], outputs: 'redgreen' },
109
+
110
+ // // Overlay 'blue' onto 'redgreen', moving it to the right
111
+ // { filter: 'overlay', options: { x: '2w', y: 0 }, inputs: ['redgreen', 'blue'] },
112
+ // ])
113
+ } )
114
+ }
115
+ }
0 commit comments