Skip to content

Commit

Permalink
Merge branch 'master' into v1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinNorgarb committed May 27, 2018
2 parents 6fe2e00 + ddb9962 commit 53667d9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
![alt text](https://devtube.devswebdev.com/img/Screenshot%20from%202018-05-26%2013-54-19.png "devbeats banner")


Install FFMPEG only if you want to convert videos to mp3 etc

Ubuntu:

```bash
sudo apt install ffmpeg
```


Install via composer

```bash
Expand Down Expand Up @@ -37,14 +46,14 @@ class YoutubeDownloadController extends Controller
{
public function download(Request $r)
{
$dl = new Download($r->url);
$dl = new Download($r->url, $format);

//Saves the file to specified directory
$dl->download();

// Return as a download
return response()->download($dl->savedPath);

}
}
```

<!-- Or if you want to Download and return the download to the view:
```php
return response()->download(storage_path(session($_SERVER['REMOTE_ADDR'])));
``` -->
62 changes: 57 additions & 5 deletions src/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,40 @@

class Download
{
/**
* [public youtube class object]
* @var [object]
*/
public $youtube;

/**
* [public download filesystem path]
* @var [string]
*/
public $path;

/**
* [public saved path dir]
* @var [string]
*/
public $savedPath;

/**
* [public youtube video url]
* @var [string]
*/
public $url;

/**
* [public whether video or audio is to be downloaded]
* @var [string]
*/
public $format;

/**
* [downloaded filename]
* @var [string]
*/
public $fileName;

/**
Expand All @@ -35,31 +59,59 @@ public function __construct($url, $format = null)


/**
* Runs the download process
* Begin the download process
* @return [string] [downloaded file path and name]
*/
public function download()
{
if ($this->format == "audio") {
$this->downloadAudio();
} else {
$this->downloadVideo();
}
}

public function downloadAudio()
{
try {
$file = new Downloader($this->url, true, 'audio');
$this->fileName = $file->audio;
return $this->save($file->audio);
} else {
} catch (\Exception $e) {
$youtube = new YoutubeDownloader($this->url);
$youtube->setPath($this->path);

$youtube->onComplete = function ($filePath, $fileSize, $index, $count) {
return $this->save(basename($filePath));
$this->save(basename($filePath));
$this->convert(basename($filePath));
};

$youtube->download();
}
}

public function downloadVideo()
{
$youtube = new YoutubeDownloader($this->url);
$youtube->setPath($this->path);

$youtube->onComplete = function ($filePath, $fileSize, $index, $count) {
return $this->save(basename($filePath));
};

$youtube->download();
}

public function convert($filePath)
{
exec("ffmpeg -i ".$this->savedPath." -vn -ar 44100 -ac 2 -ab 192000 -f mp3 ".$this->savedPath.".mp3");
$this->savedPath = $this->path."/".$filePath.".mp3";
return $this->savedPath;
}

/**
* [calls the save method]
* @param [type] $filePath [description]
* @return [type] [description]
* @param [type] $filePath [downloaded files path]
*/
public function save($filePath)
{
Expand Down

0 comments on commit 53667d9

Please sign in to comment.