Skip to content

Commit

Permalink
[#59] Replace Pafy with YT-DLP to fix download speed
Browse files Browse the repository at this point in the history
  • Loading branch information
sindre0830 committed Mar 6, 2022
1 parent df28b24 commit 168a66f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 39 deletions.
40 changes: 2 additions & 38 deletions NN/API/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import dictionary as dict
# import foreign modules
import os
from pathlib import Path
from pydub import AudioSegment
import pafy


# Downloads an audio file from given URL.
Expand All @@ -14,38 +11,5 @@ def downloadAudio(id):
os.makedirs(dict.NATIVE_DIR)
# branch if audio file doesn't exist
if not os.path.isfile(dict.getNativeAudioPath(id)):
url = "https://www.youtube.com/watch?v=" + id
audiostreams = pafy.new(url).audiostreams
# get audio format with best quality
best = 0
for idx, val in enumerate(audiostreams):
temp = int(val.get_filesize())
if best == 0 or temp > best:
best = idx
print(val.bitrate, val.extension, val.get_filesize())
tempFilename = id + "." + audiostreams[best].extension
# download audio file
if os.path.exists(dict.NATIVE_DIR + tempFilename) is False:
audiostreams[best].download(filepath=dict.NATIVE_DIR + tempFilename)
# convert file to wav format and remove temporary file
convertToWav(id, tempFilename)
os.remove(dict.NATIVE_DIR + tempFilename)


# Attempts to convert a file into wav format.
def convertToWav(id, file):
path = dict.NATIVE_DIR + file
newPath = dict.getNativeAudioPath(id)
if os.path.exists(newPath) is False and testExt(file):
sound = AudioSegment.from_file(path)
sound.export(newPath, format="wav")
else:
pass


# Checks if file is a supported audio format.
def testExt(file):
if Path(file).suffix in dict.EXTENSIONS:
return True
else:
return False
# download audio file with best quality then convert to wav
os.system("yt-dlp -q -f 'ba' -x --audio-format wav https://www.youtube.com/watch?v=" + id + " -o '"+ dict.NATIVE_DIR + "%(id)s.%(ext)s'")
2 changes: 1 addition & 1 deletion NN/API/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
flask
pydub
pafy
youtube-dl==2020.12.02
numpy==1.21
librosa
matplotlib
aubio
yt-dlp

0 comments on commit 168a66f

Please sign in to comment.