toitadesBot/main.py
2023-05-08 13:34:03 +02:00

48 lines
1.5 KiB
Python
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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)