48 lines
1.5 KiB
Python
Executable File
48 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python
|
||
|
||
import yt_dlp
|
||
import os
|
||
from pydub import AudioSegment
|
||
|
||
|
||
sounds_folder = 'Sounds'
|
||
|
||
def downloadAndCutVideo(url, filename, start = -1, end = -1):
|
||
if start == -1 and end == -1:
|
||
downloadVideo(url, filename)
|
||
return
|
||
|
||
tmpFilename = filename + "tmp"
|
||
downloadVideo(url, tmpFilename)
|
||
song = AudioSegment.from_mp3(computePath(tmpFilename))
|
||
|
||
if start != -1:
|
||
start = start * 1000
|
||
if end != -1:
|
||
end = end * 1000
|
||
cutted_song = song[start:end].export(computePath(filename), format="mp3")
|
||
os.remove(computePath(tmpFilename))
|
||
|
||
def downloadVideo(url, filename):
|
||
ydl_opts = {"format": "bestaudio", "outtmpl": sounds_folder + "/" + filename + ".%(ext)s"}
|
||
ydl_opts = {
|
||
'format': 'm4a/bestaudio/best',
|
||
# ℹ️ See help(yt_dlp.postprocessor) for a list of available Postprocessors and their arguments
|
||
'postprocessors': [{ # Extract audio using ffmpeg
|
||
'key': 'FFmpegExtractAudio',
|
||
'preferredcodec': 'mp3',
|
||
}],
|
||
"outtmpl": sounds_folder + "/" + filename + ".%(ext)s"
|
||
}
|
||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||
ydl.download([url])
|
||
|
||
def computePath(filename):
|
||
return sounds_folder + '/' + filename + ".mp3"
|
||
|
||
def refreshAvailableSounds():
|
||
|
||
|
||
if __name__ == '__main__':
|
||
downloadAndCutVideo('https://www.youtube.com/watch?v=wyNOM_HIv0U', "jesuislelaitier", 6, 10)
|