Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create file format tag --fileformat for converting audio downloads #7

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cache: pip
install:
- pip install .
- pip install pytest-cov flake8
- sudo apt-get install ffmpeg
script:
- pytest --cov=audioscrape
- flake8
Expand Down
36 changes: 28 additions & 8 deletions audioscrape/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
"""Download audio."""
import argparse
import sys
import os

from . import soundcloud, youtube


def download(query, include=None, exclude=None, quiet=False, overwrite=False):
def download(query, include=None, exclude=None, quiet=False,
overwrite=False, fileformat=None):
"""Scrape various websites for audio."""
youtube.scrape(query, include, exclude, quiet, overwrite)
soundcloud.scrape(query, include, exclude, quiet, overwrite)
# Create subdirectory for converted audio files if --fileformat tag set
if fileformat:
if not os.path.exists(fileformat):
os.makedirs(fileformat)
youtube.scrape(query, include, exclude, quiet, overwrite, fileformat)
soundcloud.scrape(query, include, exclude, quiet, overwrite, fileformat)


def cli(args=None):
Expand All @@ -20,7 +26,8 @@ def cli(args=None):
'query',
default="Cerulean Crayons",
nargs='?',
help="search terms")
help="search terms"
)
parser.add_argument(
'-i',
'--include',
Expand All @@ -40,20 +47,33 @@ def cli(args=None):
'--quiet',
default=False,
action='store_true',
help="hide progress reporting")
help="hide progress reporting"
)
parser.add_argument(
'-o',
'--overwrite',
default=False,
action='store_true',
help="overwrite existing files")
help="overwrite existing files"
)
parser.add_argument(
'-ff',
'--fileformat',
default=None,
action='store',
help="file format to save audio file as (wav, mp3, ogg)"
)
args = parser.parse_args()

if not args.quiet:
print('Downloading audio from "{}" videos tagged {} and not {}.'.
format(args.query, args.include, args.exclude))
download(args.query, args.include, args.exclude, args.quiet,
args.overwrite)
download(args.query,
args.include,
args.exclude,
args.quiet,
args.overwrite,
args.fileformat)
if not args.quiet:
print("Finished downloading audio.")

Expand Down
23 changes: 23 additions & 0 deletions audioscrape/audioconvert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# coding=utf-8
'''Convert audio clips to user defined format via ffmpeg'''
import subprocess


def ffmpeg_convert(file, audio_title, fileformat,
channel=1, sampling_rate=16000):
'''
Convert audio file to designated file type
defaults using 16kHz, mono channel wav commonly used in
training automatic speech recognition models
'''
command = ['ffmpeg', '-hide_banner', # quiet ffmpeg banner
'-loglevel', 'panic', # quiet ffmpeg stdout
'-i', "./{}".format(file), # input file to convert
'-f', '{}'.format(fileformat), # output fileformat type
'-ac', '{}'.format(channel), # mono channel default
'-ar', '{}'.format(sampling_rate), # sampling rate 16000Hz default
'-vn', # only want audio, no video
"./{0}/{1}.{0}".format(fileformat,
audio_title.replace(" ", "_"))]
subprocess.call(command)
return None
8 changes: 7 additions & 1 deletion audioscrape/soundcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import requests
import soundcloud
from tqdm import tqdm
from . import audioconvert as audc


def sanitize(s):
Expand All @@ -22,7 +23,7 @@ def sanitize(s):
API_KEY = "81f430860ad96d8170e3bf1639d4e072"


def scrape(query, include, exclude, quiet, overwrite):
def scrape(query, include, exclude, quiet, overwrite, fileformat):
"""Search SoundCloud and download audio from discovered playlists."""

# Launch SoundCloud client.
Expand Down Expand Up @@ -95,3 +96,8 @@ def pagination(x):
unit='MB',
file=sys.stdout):
f.write(data)
# Convert to fileformat using ffmpeg
if fileformat:
audc.ffmpeg_convert(file,
sanitize(track.title),
fileformat)
17 changes: 16 additions & 1 deletion audioscrape/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re

import pafy
from . import audioconvert as audc

try:
from urllib.parse import urlencode
Expand All @@ -12,7 +13,7 @@
from urllib import urlencode, urlopen


def scrape(query, include, exclude, quiet, overwrite):
def scrape(query, include, exclude, quiet, overwrite, fileformat):
"""Search YouTube and download audio from discovered videos."""

# Search YouTube for videos.
Expand Down Expand Up @@ -51,3 +52,17 @@ def scrape(query, include, exclude, quiet, overwrite):

# Download audio to working directory.
audio.download(quiet=quiet)

'''
Since pafy.Stream object (audio) does not appear to grab audio content
itself until Stream.download(), we must convert
the audio after download with ffmpeg.

Convert to fileformat using ffmpeg
'''
if fileformat:
audio_name = str(audio.title)
audio_extension = str(audio.extension)
audc.ffmpeg_convert('.'.join([audio_name, audio_extension]),
audio_name,
fileformat)
4 changes: 3 additions & 1 deletion tests/test_audioscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ def test():
query='Cerulean Crayons',
include=['guitar'],
exclude=['remix'],
quiet=False)
quiet=False,
fileformat='wav'
)