From ffbeba2c6ce1ff86ab80b3b1581bec132f8720e2 Mon Sep 17 00:00:00 2001 From: Artlef Date: Mon, 8 May 2023 13:34:03 +0200 Subject: [PATCH] First commit --- Sounds/.gitkeep | 0 main.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Sounds/.gitkeep create mode 100755 main.py diff --git a/Sounds/.gitkeep b/Sounds/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100755 index 0000000..09bc23c --- /dev/null +++ b/main.py @@ -0,0 +1,47 @@ +#!/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)