mirror of
https://github.com/THIS-IS-NOT-A-BACKUP/zspotify.git
synced 2025-07-01 07:13:14 +00:00
Added function to check song duration
- New constant in const.py: TRACK_STATS_URL = 'https://api.spotify.com/v1/audio-features/' - New function in track.py: get_song_duration(): makes a request to the API for audio features which contain the song's duration in miliseconds. Converts miliseconds to seconds. - New function in utils.py: get_downloaded_song_duration(): uses the subprocess module to run an ffmpeg command that ouputs the duration of the song file in seconds. Output is captured and returned as float.
This commit is contained in:
parent
c5f4bf1ea6
commit
97f8321877
@ -2,6 +2,8 @@ SAVED_TRACKS_URL = 'https://api.spotify.com/v1/me/tracks'
|
|||||||
|
|
||||||
TRACKS_URL = 'https://api.spotify.com/v1/tracks'
|
TRACKS_URL = 'https://api.spotify.com/v1/tracks'
|
||||||
|
|
||||||
|
TRACK_STATS_URL = 'https://api.spotify.com/v1/audio-features/'
|
||||||
|
|
||||||
TRACKNUMBER = 'tracknumber'
|
TRACKNUMBER = 'tracknumber'
|
||||||
|
|
||||||
DISCNUMBER = 'discnumber'
|
DISCNUMBER = 'discnumber'
|
||||||
|
@ -10,8 +10,8 @@ from pydub import AudioSegment
|
|||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
from const import TRACKS, ALBUM, NAME, ITEMS, DISC_NUMBER, TRACK_NUMBER, IS_PLAYABLE, ARTISTS, IMAGES, URL, \
|
from const import TRACKS, ALBUM, NAME, ITEMS, DISC_NUMBER, TRACK_NUMBER, IS_PLAYABLE, ARTISTS, IMAGES, URL, \
|
||||||
RELEASE_DATE, ID, TRACKS_URL, SAVED_TRACKS_URL, SPLIT_ALBUM_DISCS, ROOT_PATH, DOWNLOAD_FORMAT, CHUNK_SIZE, \
|
RELEASE_DATE, ID, TRACKS_URL, SAVED_TRACKS_URL, TRACK_STATS_URL, SPLIT_ALBUM_DISCS, ROOT_PATH, DOWNLOAD_FORMAT, \
|
||||||
SKIP_EXISTING_FILES, ANTI_BAN_WAIT_TIME, OVERRIDE_AUTO_WAIT, BITRATE, CODEC_MAP, EXT_MAP, DOWNLOAD_REAL_TIME
|
CHUNK_SIZE, SKIP_EXISTING_FILES, ANTI_BAN_WAIT_TIME, OVERRIDE_AUTO_WAIT, BITRATE, CODEC_MAP, EXT_MAP, DOWNLOAD_REAL_TIME
|
||||||
from utils import fix_filename, set_audio_tags, set_music_thumbnail, create_download_directory, \
|
from utils import fix_filename, set_audio_tags, set_music_thumbnail, create_download_directory, \
|
||||||
get_directory_song_ids, add_to_directory_song_ids
|
get_directory_song_ids, add_to_directory_song_ids
|
||||||
from zspotify import ZSpotify
|
from zspotify import ZSpotify
|
||||||
@ -52,6 +52,21 @@ def get_song_info(song_id) -> Tuple[List[str], str, str, Any, Any, Any, Any, Any
|
|||||||
|
|
||||||
return artists, album_name, name, image_url, release_year, disc_number, track_number, scraped_song_id, is_playable
|
return artists, album_name, name, image_url, release_year, disc_number, track_number, scraped_song_id, is_playable
|
||||||
|
|
||||||
|
def get_song_duration(song_id: str) -> float:
|
||||||
|
""" Retrieves duration of song in second as is on spotify """
|
||||||
|
|
||||||
|
resp = ZSpotify.invoke_url(f'{TRACK_STATS_URL}{song_id}')
|
||||||
|
|
||||||
|
# get duration in miliseconds
|
||||||
|
ms_duration = resp['duration_ms']
|
||||||
|
# convert to seconds
|
||||||
|
duration = float(ms_duration)/1000
|
||||||
|
|
||||||
|
# debug
|
||||||
|
print(duration)
|
||||||
|
print(type(duration))
|
||||||
|
|
||||||
|
return duration
|
||||||
|
|
||||||
# noinspection PyBroadException
|
# noinspection PyBroadException
|
||||||
def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', disable_progressbar=False) -> None:
|
def download_track(track_id: str, extra_paths='', prefix=False, prefix_value='', disable_progressbar=False) -> None:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import re
|
import re
|
||||||
|
import subprocess
|
||||||
import time
|
import time
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple
|
||||||
@ -48,6 +49,20 @@ def add_to_directory_song_ids(download_path: str, song_id: str) -> None:
|
|||||||
with open(hidden_file_path, 'a', encoding='utf-8') as file:
|
with open(hidden_file_path, 'a', encoding='utf-8') as file:
|
||||||
file.write(f'{song_id}\n')
|
file.write(f'{song_id}\n')
|
||||||
|
|
||||||
|
def get_downloaded_song_duration(filename: str) -> float:
|
||||||
|
""" Returns the downloaded file's duration in seconds """
|
||||||
|
|
||||||
|
command = ['ffprobe', '-show_entries', 'format=duration', '-i', f'{filename}']
|
||||||
|
output = subprocess.run(command, capture_output=True)
|
||||||
|
|
||||||
|
print(output.stdout)
|
||||||
|
|
||||||
|
duration = re.search(r'[\D]=([\d\.]*)', str(output.stdout)).groups()[0]
|
||||||
|
duration = float(duration)
|
||||||
|
|
||||||
|
print(duration)
|
||||||
|
return duration
|
||||||
|
|
||||||
def wait(seconds: int = 3) -> None:
|
def wait(seconds: int = 3) -> None:
|
||||||
""" Pause for a set number of seconds """
|
""" Pause for a set number of seconds """
|
||||||
for second in range(seconds)[::-1]:
|
for second in range(seconds)[::-1]:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user