Skip to content

Instantly share code, notes, and snippets.

@benkaiser
Created May 29, 2014 07:28
Show Gist options
  • Select an option

  • Save benkaiser/9e759eae846c672df92a to your computer and use it in GitHub Desktop.

Select an option

Save benkaiser/9e759eae846c672df92a to your computer and use it in GitHub Desktop.
Download youtube video, convert it to mp3 and tag it
var ffmpeg = require("fluent-ffmpeg");
var taglib = require("taglib");
var ytdl = require("ytdl");
var fs = require("fs");
var url = 'http://www.youtube.com/watch?v=gXK5YpoadCU';
var title = "";
var authot = "";
ytdl.getInfo(url, function(err, info) {
if (err) return done(err);
console.log(info.title);
console.log(info.author);
title = info.title;
author = info.author;
});
fs.exists("video.mp4", function(exists){
if(!exists){
var dl = ytdl(url,
{ filter: function(format) { return format.container === 'mp4'; } });
dl.pipe(fs.createWriteStream('video.mp4'));
dl.on('end', function () {
console.log('File Saved');
doConversion('video.mp4', 'audio.mp3');
});
} else {
doConversion('video.mp4', 'audio.mp3');
}
});
function doConversion(inFile, outFile){
console.log("Starting conversion");
var command = new ffmpeg({source: inFile})
.on('start', function(commandLine) {
console.log('Spawned FFmpeg with command: ' + commandLine);
})
.on('end', function() {
console.log('Processing finished successfully');
taglib.tag(outFile, function(err, tag){
tag.title = title;
tag.artist = author;
console.log(tag);
tag.saveSync();
})
})
.withNoVideo()
.toFormat('mp3')
.withAudioBitrate('128k')
.saveToFile(outFile);
}
{
"name": "youtube-dl-ogg-testing",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"ytdl": "*",
"fluent-ffmpeg": "*",
"taglib": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment