iawia002 / lux

👾 Fast and simple video download library and CLI tool written in Go
MIT License
27.42k stars 2.96k forks source link

Audio only from youtube. #33

Closed abhimanyu003 closed 6 years ago

abhimanyu003 commented 6 years ago

Hello,

Is there any we can only download audio from Youtube in mp3 format. ?

Thanks

iawia002 commented 6 years ago

If the api data of youtube has audio file itself, then I think Annie can do it.

lamg commented 6 years ago

Being able to see all available formats and then downloading a specific one would be nice.

iawia002 commented 6 years ago

@lamg you can draft a new issue and we discuss there

John128217 commented 6 years ago

I saw you grab the source code of youtube video here:

videoURL := fmt.Sprintf( "https://www.youtube.com/watch?v=%s&gl=US&hl=en&has_verified=1&bpctr=9999999999", vid[1], ) html := request.Get(videoURL) ytplayer := utils.MatchOneOf(html, ;ytplayer\.config\s*=\s*({.+?});)[1] var youtube youtubeData json.Unmarshal([]byte(ytplayer), &youtube) title := youtube.Args.Title streams := strings.Split(youtube.Args.Stream, ",")

I have a question: What is Args.Stream?

"adaptive_fmts" in ytplayer.config.args contains audio urls that you can use.

iawia002 commented 6 years ago

@John128217 Args.Stream is url_encoded_fmt_stream_map in ytplayer.config.args

I have checked adaptive_fmts, I got this:

index=715-3182&init=0-714&fps=30&quality_label=1080p&projection_type=1&lmt=1515503341429675&size=1920x1080&xtags=&clen=163848910&url=https://r5---sn-oguelnle.googlevideo.com/videoplayback?signature=210AFCFD5A5DDA5C7C1B411C4C055F1D3F3ADED4.4A9C0282B74B856F8F594672FF38E09DE03E133F&lmt=1515503341429675&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278&key=yt6&itag=137&mime=video%2Fmp4&ipbits=0&dur=1031.096&ms=au%2Conr&mv=m&mt=1521850392&mn=sn-oguelnle%2Csn-3pm7sn7y&clen=163848910&source=youtube&c=WEB&sparams=aitags%2Cclen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cexpire&id=o-ALDakTy85mQPQvpeGgxh99D1A5UuXW0tvDNv6ThjRZRa&mm=31%2C26&initcwndbps=2178750&ip=153.122.92.144&ei=nJi1WsabHcqWqQHU7YWQBw&requiressl=yes&pl=20&keepalive=yes&fvip=5&gir=yes&expire=1521872124&type=video/mp4; codecs="avc1.640028"&bitrate=3924447&itag=137
elig0n commented 6 years ago

I would like that too. meanwhile one can use a video to audio converter

John128217 commented 6 years ago

@iawia002 adaptive_fmts is definitely longer than that... Video and audio are separated as two different files inside adaptive_fmts. You can search "audio/mp4" or "audio/webm" or "audio%2Fmp4" or "audio%2Fwebm" for audio. For example:

clen=3376450\u0026itag=140\u0026sp=signature\u0026init=0-591\u0026url=https%3A%2F%2Fr3---sn-a5msen7s.googlevideo.com%2Fvideoplayback%3Fitag%3D140%26mime%3Daudio%252Fmp4%26dur%3D212.532%26mt%3D1521919865%26pl%3D14%26mv%3Dm%26ei%3DC6i2Wvz-DtHY-AOB1YrQCw%26id%3Do-AN1jphbV6zVTsQyMGb_neuFkQZ3yfRKILY1SorHfqj7A%26ms%3Dau%252Crdu%26fvip%3D4%26ip%3D**.**.**.**%26gir%3Dyes%26mm%3D31%252C29%26source%3Dyoutube%26mn%3Dsn-a5msen7s%252Csn-a5mekn7d%26ipbits%3D0%26sparams%3Dclen%252Cdur%252Cei%252Cgir%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Ckeepalive%252Clmt%252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpl%252Crequiressl%252Csource%252Cexpire%26lmt%3D1509072367449586%26clen%3D3376450%26expire%3D1521941611%26key%3Dyt6%26initcwndbps%3D1060000%26requiressl%3Dyes%26c%3DWEB%26keepalive%3Dyes\u0026bitrate=128016\u0026type=audio%2Fmp4%3B+codecs%3D%22mp4a.40.2%22\u0026index=592-887\u0026s=E83E838C3AE75F1E9EE0DF9C741E9830B59E4D1D934.9F8C88F34F10A5132CF2B57AFD8F7E1DB7780EC0EC0EC0\u0026lmt=1509072367449586\u0026projection_type=1\u0026xtags=

Here's my code in javascript for grabbing audio:

  var audio, video=[], decode=function(r){for(var t=[],a=0;a<r.length;a++){for(var n=r[a].split("&"),l={},v=0;v<n.length;v++){var e=n[v].split("=");l[e[0]]=e[1]}t.push(l)}return t}; 
  //decode function should be url.ParseQuery in go 
  if ('adaptive_fmts' in config['args']){
    audio=decode(config['args']['adaptive_fmts'].split(','));
    for (streamnum in audio){
      if (decodeURIComponent(audio[streamnum]['type']).slice(0,9)!='audio/mp4')
        continue;
      audio[streamnum]['url']+='&ratebypass=yes';
      audio[streamnum]['quality']='audio';
      video.push(audio[streamnum]);
      break;
    }
  }
  //I'm still a beginner so this piece of code might not be good

You will finally get a m4a file. It's also easy to convert it using ffmpeg.

iawia002 commented 6 years ago

thanks @John128217 I'll take a look.