Advertisement
Guest User

Untitled

a guest
Nov 18th, 2023
6,128
1
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 1 0
  1. from pydub import AudioSegment, effects
  2. from yt_dlp import YoutubeDL
  3. import glob
  4. import isodate
  5. import os
  6. import random
  7. import subprocess
  8. import wave
  9.  
  10. BATCH_SIZE = 32
  11. MAX_SEARCH_RESULTS = 10
  12. DOWNLOAD_DIR = 'wavs/raw'
  13. LOOP_OUTPUT_DIR = 'wavs/processed/loop'
  14. ONESHOT_OUTPUT_DIR = 'wavs/processed/oneshot'
  15. WORD_LIST = 'words.txt'
  16.  
  17. def read_lines(file):
  18. return open(file).read().splitlines()
  19.  
  20. class download_range_func:
  21. def __init__(self):
  22. pass
  23. def __call__(self, info_dict, ydl):
  24. timestamp = self.make_timestamp(info_dict)
  25. yield {
  26. 'start_time': timestamp,
  27. 'end_time': timestamp,
  28. }
  29. @staticmethod
  30. def make_timestamp(info):
  31. duration = info['duration']
  32. if duration is None:
  33. return 0
  34. return duration/2
  35.  
  36. def make_random_search_phrase(word_list):
  37. words = random.sample(word_list, 2)
  38. phrase = ' '.join(words)
  39. print('Search phrase: "{}"'.format(phrase))
  40. return phrase
  41.  
  42. def make_download_options():
  43. return {
  44. 'format': 'bestaudio/best',
  45. 'paths': {'home': DOWNLOAD_DIR},
  46. 'outtmpl': {'default': '%(id)s.%(ext)s'},
  47. 'download_ranges': download_range_func(),
  48. 'postprocessors': [{
  49. 'key': 'FFmpegExtractAudio',
  50. 'preferredcodec': 'wav',
  51. }]
  52. }
  53.  
  54. def make_oneshot(sound, output_filepath):
  55. final_length = min(2000, len(sound))
  56. quarter = int(final_length/4)
  57. sound = sound[:final_length]
  58. sound = sound.fade_out(duration=quarter)
  59. sound = effects.normalize(sound)
  60. sound.export(output_filepath, format="wav")
  61.  
  62. def make_loop(sound, output_filepath):
  63. final_length = min(2000, len(sound))
  64. half = int(final_length/2)
  65. fade_length = int(final_length/4)
  66. beg = sound[:half]
  67. end = sound[half:]
  68. end = end[:fade_length]
  69. beg = beg.fade_in(duration=fade_length)
  70. end = end.fade_out(duration=fade_length)
  71. sound = beg.overlay(end)
  72. sound = effects.normalize(sound)
  73. sound.export(output_filepath, format="wav")
  74.  
  75. def process_file(filepath):
  76. try:
  77. filename = os.path.basename(filepath)
  78. output_filepath_oneshot = os.path.join(ONESHOT_OUTPUT_DIR, 'oneshot_' + filename)
  79. output_filepath_loop = os.path.join(LOOP_OUTPUT_DIR, 'loop_' + filename)
  80. sound = AudioSegment.from_file(filepath, "wav")
  81. if (len(sound) > 500):
  82. if not os.path.exists(output_filepath_oneshot):
  83. make_oneshot(sound, output_filepath_oneshot)
  84. if not os.path.exists(output_filepath_loop):
  85. make_loop(sound, output_filepath_loop)
  86. os.remove(filepath)
  87. except Exception as err:
  88. print("Failed to process '{}' ({})".format(filepath, err))
  89.  
  90. def setup():
  91. if not os.path.exists(LOOP_OUTPUT_DIR):
  92. os.makedirs(LOOP_OUTPUT_DIR)
  93. if not os.path.exists(ONESHOT_OUTPUT_DIR):
  94. os.makedirs(ONESHOT_OUTPUT_DIR)
  95.  
  96. def main():
  97. try:
  98. setup()
  99. word_list = read_lines(WORD_LIST)
  100. for _ in range(BATCH_SIZE):
  101. phrase = make_random_search_phrase(word_list)
  102. video_url = 'ytsearch1:"{}"'.format(phrase)
  103. YoutubeDL(make_download_options()).download([video_url])
  104. for filepath in glob.glob(os.path.join(DOWNLOAD_DIR, '*.wav')):
  105. process_file(filepath)
  106. except Exception as err:
  107. print('FATAL ERROR: {}'.format(err))
  108.  
  109. if __name__ == '__main__':
  110. main()
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement