Download while simultaneously encoding

This commit is contained in:
Ritiek Malhotra
2020-03-23 18:29:23 +05:30
parent 7413c541d3
commit d154b2be20
4 changed files with 90 additions and 2 deletions

View File

@@ -1 +1,3 @@
__version__ = "1.2.6"
from spotdl.download import Track

66
spotdl/download.py Normal file
View File

@@ -0,0 +1,66 @@
import tqdm
import subprocess
import urllib.request
from spotdl.encode.encoders import EncoderFFmpeg
CHUNK_SIZE= 16 * 1024
HEADERS = [('Range', 'bytes=0-'),]
class Track:
def __init__(self, metadata):
self.metadata = metadata
self.network_headers = HEADERS
self._chunksize = CHUNK_SIZE
def _make_request(self, url):
request = urllib.request.Request(url)
for header in self.network_headers:
request.add_header(*header)
return urllib.request.urlopen(request)
def _calculate_total_chunks(self, filesize):
return (filesize // self._chunksize) + 1
def download_while_re_encoding(self, path, encoder=EncoderFFmpeg(), show_progress=True):
stream = self.metadata["streams"].getbest()
total_chunks = self._calculate_total_chunks(stream["filesize"])
response = self._make_request(stream["download_url"])
process = encoder.re_encode_from_stdin(
stream["encoding"],
path
)
for _ in tqdm.trange(total_chunks):
chunk = response.read(self._chunksize)
process.stdin.write(chunk)
process.stdin.close()
process.wait()
def download(self, path, show_progress=True):
stream = self.metadata["streams"].getbest()
total_chunks = self._calculate_total_chunks(stream["filesize"])
response = self._make_request(stream["download_url"])
with open(path, "wb") as fout:
for _ in tqdm.trange(total_chunks):
chunk = response.read(self._chunksize)
fout.write(chunk)
def re_encode(self, input_path, target_path, encoder=EncoderFFmpeg(), show_progress=True):
stream = self.metadata["streams"].getbest()
total_chunks = self._calculate_total_chunks(stream["filesize"])
process = encoder.re_encode_from_stdin(
stream["encoding"],
target_path
)
with open(input_path, "rb") as fin:
for _ in tqdm.trange(total_chunks):
chunk = fin.read(self._chunksize)
process.stdin.write(chunk)
process.stdin.close()
process.wait()
def apply_metadata(path):
pass

View File

@@ -13,7 +13,7 @@ RULES = {
"m4a": "-acodec copy",
"flac": "-codec:a flac -ar 48000",
},
"webm": {
"opus": {
"mp3": "-codec:a libmp3lame -ar 48000",
"m4a": "-cutoff 20000 -codec:a aac -ar 48000",
"flac": "-codec:a flac -ar 48000",
@@ -99,6 +99,6 @@ class EncoderFFmpeg(EncoderBase):
output_file,
input_encoding=input_encoding,
)
process = subprocess.Popen(encode_command)
process = subprocess.Popen(encode_command, stdin=subprocess.PIPE)
return process

20
spotdl/helpers.py Normal file
View File

@@ -0,0 +1,20 @@
import subprocess
import threading
def download(url, path=".", progress_bar=True, ):
command = ("ffmpeg", "-y", "-i", "-", "output.wav")
content = pytube.YouTube("https://www.youtube.com/watch?v=SE0nYFJ0ZvQ")
stream = content.streams[0]
response = urllib.request.urlopen(stream.url)
process = subprocess.Popen(command, stdin=subprocess.PIPE)
while True:
chunk = response.read(16 * 1024)
if not chunk:
break
process.stdin.write(chunk)
process.stdin.close()