├── README.md └── patch-r1.c /README.md: -------------------------------------------------------------------------------- 1 | # cmus-youtube 2 | A patch that allows you to download and play youtube songs using cmus 3 | 4 | # How to use ? 5 | First , you have to download cmus code source , and build them . 6 | 7 | $ git clone https://github.com/cmus/cmus.git 8 | 9 | then go to cmus directory . Open the file command_mode.c , and add the content of patch-r1.c . 10 | 11 | add this 12 | 13 | { "youtube", cmd_youtube, 1, 1, NULL, 0, 0 }, 14 | 15 | after 16 | 17 | { "wq", cmd_quit, 0, 1, NULL, 0, 0 }, 18 | 19 | save the command_mode.c file 20 | 21 | and configure cmus . 22 | 23 | List available optional features 24 | 25 | $ ./configure --help 26 | 27 | Auto-detect everything 28 | 29 | $ ./configure 30 | 31 | If you get a 'configure failed' error, run 32 | 33 | $ sudo apt-get install libncurses5-dev 34 | 35 | To disable some feature, arts for example, and install to $HOME run 36 | 37 | $ ./configure prefix=$HOME CONFIG_ARTS=n 38 | 39 | Now open the Makefile and go to line 19 40 | 41 | CFLAGS += -D_FILE_OFFSET_BITS=64 42 | 43 | add this 44 | 45 | -D_YOUTUBE_DL=1 46 | 47 | then build the source using 48 | 49 | $ make 50 | 51 | open cmus using 52 | 53 | $ ./cmus 54 | 55 | #Command Line 56 | You can use cmus-youtube by using the command line : 57 | 58 | :youtube 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /patch-r1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008-2013 Various Authors 3 | * Copyright 2004-2005 Timo Hirvonen 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License as 7 | * published by the Free Software Foundation; either version 2 of the 8 | * License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, but 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, see . 17 | */ 18 | /* 19 | @cmus-youtube . patch-r1 20 | @author : hamza naper 21 | */ 22 | #ifdef linux 23 | #include 24 | #include 25 | #endif 26 | static void ydl_createConf(char path[1025], char * username){ 27 | FILE * conf = NULL; 28 | char pwd[1025]; 29 | struct stat st = {0}; 30 | 31 | conf = fopen(path,"a+"); 32 | if(conf){ 33 | /* write to file */ 34 | fprintf(conf,"#cmus-ydl configuration file \n"); 35 | fprintf(conf,"ydl-dir=/home/%s/.cmus-youtube/\n",username); 36 | fprintf(conf,"ydl-extention=mp3\n"); 37 | fprintf(conf,"ydl-name=song\n"); 38 | 39 | /* create a directory */ 40 | snprintf(pwd,sizeof(pwd),"/home/%s/.cmus-youtube",username); 41 | if (stat(pwd, &st) == -1) { 42 | mkdir(pwd, 0700); 43 | } 44 | 45 | fclose(conf); 46 | } 47 | 48 | return ; 49 | } 50 | 51 | static void ydl_conf(const char * element){ 52 | /* check if config , is available 53 | if not available , we will create one 54 | */ 55 | FILE * conf = NULL; 56 | char path[1025]; 57 | char * username = NULL; 58 | 59 | username = (char * )malloc(sizeof(char)); 60 | username = getlogin(); 61 | snprintf(path,sizeof(path),"/home/%s/.cmus-ydl.conf",username); 62 | 63 | conf = fopen(path,"r"); 64 | if(!conf){ 65 | /* create file */ 66 | ydl_createConf(path, username); 67 | }else{ 68 | /* read elem from config */ 69 | fclose(conf); 70 | } 71 | 72 | if(username){ 73 | //free(username); 74 | //username = NULL; 75 | } 76 | 77 | return ; 78 | } 79 | 80 | static void ydl_getStr_fromF(const char * file, char string[1025]){ 81 | FILE * fp = NULL; 82 | char result[1025]; 83 | 84 | fp = fopen(file,"r"); 85 | if(!fp){ 86 | error_msg("error while checking a tmp file"); 87 | }else{ 88 | fscanf(fp,"%s\n",result); 89 | strcpy(string, result); 90 | fclose(fp); 91 | } 92 | 93 | return ; 94 | } 95 | 96 | static void cmd_youtube(char *arg){ 97 | /* 98 | This function allows to download a music from youtube and play it 99 | downloading .mp4 from youtube 100 | Using Avconv CBR to convert .mp4 to .mp3 101 | */ 102 | #ifdef _YOUTUBE_DL 103 | #ifdef linux 104 | char version[1025]; 105 | char cmd[1025]; 106 | char link[1025]; 107 | 108 | /* check config */ 109 | ydl_conf(NULL); 110 | system("youtube-dl -v > /tmp/cmus-ydl.check"); 111 | /* clear system error output , and refresh */ 112 | clearok(curscr, TRUE); 113 | refresh(); 114 | 115 | ydl_getStr_fromF("/tmp/cmus-ydl.check",version); 116 | /* we will read file and get the version of youtube-dl 117 | if youtube-dl is not installed . we show an error msg to the user 118 | so as to install youtube-dl 119 | */ 120 | if(strcmp(version,"") == 0){ 121 | /* youtube-dl not installed */ 122 | error_msg("youtube-dl not installed, install it using apt-get install youtube-dl"); 123 | }else{ 124 | snprintf(cmd,sizeof(cmd),"youtube-dl -g %s > /tmp/cmus-ydl.link", arg); 125 | system(cmd); 126 | clearok(curscr, TRUE); 127 | refresh(); 128 | /* Downlaod and convert the .mp4 file */ 129 | ydl_getStr_fromF("/tmp/cmus-ydl.link",link); 130 | error_msg("link %s => %s\n",arg,link); 131 | if(strcmp(link,"") == 0){ 132 | error_msg("can't open '%s'",arg); 133 | }else{ 134 | snprintf(cmd,sizeof(cmd),"wget \"%s\" -O video.mp4",link); 135 | info_msg("Downloading..please wait"); 136 | system(cmd); 137 | clearok(curscr, TRUE); 138 | refresh(); 139 | info_msg("File downloaded! converting .."); 140 | /* check if avconv is installed */ 141 | system("avconv > /tmp/cmus-ydl.check"); 142 | ydl_getStr_fromF("/tmp/cmus-ydl.check",version); 143 | if(strcmp(version,"") == 0){ 144 | clearok(curscr, TRUE); 145 | refresh(); 146 | error_msg("avconv not installed, install it using apt-get install avconv"); 147 | }else{ 148 | /* convert file */ 149 | snprintf(cmd,sizeof(cmd),"yes | avconv -i video.mp4 -vn -acodec libmp3lame -ac 2 -ab 160k -ar 48000 song.mp3"); 150 | system(cmd); 151 | clearok(curscr, TRUE); 152 | refresh(); 153 | info_msg("converted ! song.mp3"); 154 | } 155 | } 156 | } 157 | 158 | #else 159 | error_msg("no Windows version !"); 160 | #endif 161 | #else 162 | error_msg("youtube-dl not supported !"); 163 | #endif 164 | return ; 165 | } 166 | --------------------------------------------------------------------------------