Thursday, 29 December 2016

Extracting audio from video and generating a cuesheet from a tracklist

I quite often listen to mixes on Youtube. Only problem is, you need an internet connection to listen to the music and on some videos the mix seems to be constanty interupted by ads.

The solution to this is download the video - there are plenty of free Youtube video download tools available. But then you have a video file that's much larger than it needs to be (because you only want the audio track), and can't skip from one track in the mix to another.

(If you feel guilty about downloading the video you can always donate a few dollars to Google and a few cents to the video uploader and ASCAP or the equivalent organisation in your country to make up for the lost ad revenue).

So to this end I decided to write a script that could be used to extract the audio from the MP4 video file and generate a cuesheet based on the tracklisting (copied from the video description). And I thought I might as well share it here.

For the initial setup you'll need MP4Box, which comes as part of GPAC.

Ensure GPAC is in your Path: System > Advanced System Settings > Advanced tab > Environment Variables... In User variables find Variable Path. Select it, click Edit, and add to the end: ;C:\Program Files\GPAC (Assuming that's where you installed GPAC to).

The scripts I created using powershell, so you need to allow executing powershell scripts. Run Powershell as admin. Then enter: Set-ExecutionPolicy Remote Signed.

Download the scripts: create-cue.ps1 and video-convert.ps1.

Now you need to allow running those scripts without signatures. From powershell:

Unblock-File create-cue.ps1
Unblock-File video-convert.ps1

That's the initial setup done, which you shouldn't need to do again. Now onto converting the video file.

To convert the video you need a folder containing the video file in mp4 format and a tracklist file. The tracklist file should be a plain text file containing lines in the format:

00:00 Artist Name - Song Title

Many (though sadly not all) Youtubers who upload mixes will have a tracklist in this format as part of the video description, so you can copy-paste from there.

The tracklist file and the video file should both have the same filename (with .txt and .mp4 extensions respectively), which should be in the format:

Album Artist - Album Title

You can run script in several ways:

  • From powershell: path\to\video-convert.ps1 \path\to\video.mp4
  • From powershell:
    cd \path\to\video
    path\to\video-convert.ps1
  • Copy both .ps1 files to the video dir, then double-click on video-convert.ps1 to run it. (This is easiet IMO).

The script will extract audio from the mp4 video, wrap it in an mp4 (m4a) wrapper, and generate a cuesheet from the tracklist file. Because it just extracts the audio, not converting it, there is no loss in audio quality.

If you want the track split into separate files, there are various cue splitters available that will split an audio file based on a cuesheet.