Skip to content

Commit

Permalink
feat: 🎸 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMikeladze committed Feb 17, 2022
0 parents commit 22c4088
Show file tree
Hide file tree
Showing 13 changed files with 6,287 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"env": {
"es2021": true,
"node": true,
"jest": true
},
"extends": [
"standard"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 13,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
]
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
dist
.idea
yarn-error.log
coverage
.env
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn test && yarn lint-staged
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Tim Mikeladze

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# super-youtube-mp3

## Installation

> yarn add super-youtube-mp3
## Usage
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/node_modules/', '__tests__/migrations/'],
setupFiles: ['dotenv/config'],
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)']
}
66 changes: 66 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "super-youtube-mp3",
"version": "0.0.1",
"description": "",
"author": "Tim Mikeladze <[email protected]>",
"keywords": [],
"repository": {
"type": "git",
"url": "https://github.com/TimMikeladze/super-youtube-mp3.git"
},
"license": "MIT",
"files": [
"dist"
],
"type": "module",
"source": "src/index.ts",
"exports": {
"require": "dist/index.cjs",
"default": "dist/index.modern.js"
},
"main": "dist/index.cjs",
"module": "dist/index.module.js",
"unpkg": "dist/index.umd.js",
"bin": "dist/cli.modern.js",
"types": "dist/index.d.ts",
"scripts": {
"dev": "microbundle watch src/{index,cli}.ts",
"build": "microbundle src/{index,cli}.ts",
"lint": "eslint --fix {src,__tests__}/**/*.ts",
"test": "jest --coverage"
},
"lint-staged": {
"*.ts": "eslint --fix"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"devDependencies": {
"@types/fluent-ffmpeg": "^2.1.20",
"@types/jest": "27.4.0",
"@typescript-eslint/eslint-plugin": "5.11.0",
"@typescript-eslint/parser": "5.11.0",
"eslint": "8.8.0",
"eslint-config-standard": "16.0.3",
"eslint-plugin-import": "2.25.4",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "6.0.0",
"husky": "7.0.4",
"jest": "27.5.1",
"lint-staged": "12.3.3",
"microbundle": "0.14.2",
"ts-jest": "27.1.3",
"typescript": "4.5.5"
},
"dependencies": {
"commander": "9.0.0",
"ffmpeg-static": "^4.4.1",
"fluent-ffmpeg": "^2.1.2",
"get-artist-title": "^1.3.1",
"get-youtube-chapters": "^1.1.0",
"node-fetch": "3.2.0",
"ytdl-core": "^4.10.1"
}
}
20 changes: 20 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": [
"config:base"
],
"timezone": "America/Los_Angeles",
"schedule": ["on the first day of the month"],
"packageRules": [
{
"matchPackagePatterns": [
"*"
],
"matchUpdateTypes": [
"minor",
"patch"
],
"groupName": "all non-major dependencies",
"groupSlug": "all-minor-patch"
}
]
}
86 changes: 86 additions & 0 deletions src/SuperSplitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import ytdl, { Chapter, chooseFormatOptions } from 'ytdl-core'
import * as fs from 'fs'
import * as path from 'path'
import ffmpeg from 'fluent-ffmpeg'
import getArtistTitle from 'get-artist-title'

export interface SuperSplitterOptions {
}

export interface ExtendedChapter extends Chapter {
end_time?: number
}

export const addChapterEndTimes = (lengthSeconds: number, chapters: Chapter[]): ExtendedChapter[] => {
if (chapters.length === 1) {
return [{
...chapters[0],
end_time: lengthSeconds
}]
}
return chapters.map((x, index) => {
if (index === chapters.length - 1) {
return {
...x,
end_time: lengthSeconds
}
} else {
return {
...x,
end_time: chapters[index + 1].start_time
}
}
})
}

export class SuperSplitter {
private readonly options: SuperSplitterOptions

constructor (options: SuperSplitterOptions) {
this.options = options
}

public async download (url: string, output: string, quality: chooseFormatOptions['quality']): Promise<void> {
const folderPath = path.resolve(output)
const tempFilePath = path.join(folderPath, 'temp.mp4')

const info = await ytdl.getInfo(url, {})

const artistName = getArtistTitle(info.videoDetails.title)

const chapters = addChapterEndTimes(Number(info.videoDetails.lengthSeconds), info.videoDetails.chapters)

const downloadVideo = async (url, path): Promise<void> => {
return new Promise((resolve, reject) => {
const video = ytdl(url, {
quality: quality || 'highestaudio'
})
.on('error', reject)

video.pipe(fs.createWriteStream(tempFilePath))

video.on('finish', () => {
resolve()
})
})
}

await downloadVideo(url, tempFilePath)

await Promise.allSettled(chapters.map(async (chapter, index) => {
return new Promise<void>((resolve, reject) => {
ffmpeg(tempFilePath).outputOptions([
'-vn',
'-i', tempFilePath,
'-ss', String(chapter.start_time),
'-t', String(chapter.end_time - chapter.start_time)
]).on('error', err => {
reject(err)
}).pipe(fs.createWriteStream(path.join(folderPath, `${artistName} - ${index + 1} - ${chapter.title}.mp3`)))
.on('end', () => {
resolve()
})
})
}))
}
}
39 changes: 39 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

import { Command } from 'commander'
import { SuperSplitter } from './SuperSplitter'

interface CommanderOptions {
url: string
quality?: string
output: string
}

const program = new Command()

program.option('-o, --output <path>', 'output directory path')
program.option('-q, --quality <quality>', 'URL of YT video')
program.option('-u, --url <url>', 'URL of YT video')

program.parse()

program.parse(process.argv)

const options: CommanderOptions = program.opts()

if (!options.url) {
console.error('url is required')
process.exit(1)
}

if (!options.output) {
console.error('output directory path is required')
process.exit(1)
}

const superSplitter = new SuperSplitter({
})

superSplitter.download(options.url, options.output).then(() => {
process.exit(0)
})
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SuperSplitter'
5 changes: 5 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"esModuleInterop": true
}
}
Loading

0 comments on commit 22c4088

Please sign in to comment.