import csv import subprocess import matplotlib.pyplot as plt import os import random import matplotlib.font_manager as fm # Create a list to store the names of the video files video_files = [] # Parse the data with open('fra.txt') as f: data = list(csv.reader(f, delimiter='\t')) for i in range(200): # Select a random row row = random.choice(data) english, other_lang, attribution = row # Generate audio for English and French #-v # Use voice file of this name from espeak-ng-data/voices #-w #Write speech to this WAV file, rather than speaking it directly subprocess.run(['espeak-ng', '-v', 'en', '-s', '120', '-w', 'english.wav', english]) subprocess.run(['espeak-ng', '-v', 'fr-fr', '-s', '70', '-w', 'french.wav', other_lang]) # Create flash card # Create flash card # figure coordinates #(0, 0) refers to the bottom-left corner of the figure. #(1, 1) refers to the top-right corner of the figure. #(0.5, 0.5) refers to the center of the figure. #sudo apt-get install fonts-noto-color-emoji #fc-cache -f -v [update font cache for emoji] prop = fm.FontProperties(fname='/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf') fig, ax = plt.subplots(figsize=(5,3)) # Adjusted for better visibility ax.text(0, 1, f'[ENG] {english}', fontsize=12, color='white', wrap=True) # Increased font size ax.text(0, 0.5, f'[FR] {other_lang}', fontsize=12, color='yellow', wrap=True) # Increased font size ax.text(0, 0.1, f' {attribution}', fontsize=12, color='green', wrap=True) # Increased font size ax.axis('off') # Remove axes fig.patch.set_facecolor('black') # Set background to black #ax.axis('tight') # Remove padding plt.savefig('flash_card.png', pad_inches=0, facecolor=fig.get_facecolor()) # Combine audio subprocess.run(['ffmpeg', '-i', 'english.wav', '-i', 'french.wav', '-filter_complex', '[0:a][1:a]concat=n=2:v=0:a=1[out]', '-map', '[out]', 'combined.wav']) #and image output_file = f'output{i}.mp4' #To create a video that displays the image for the duration of the audio, #you can use the -loop option with ffmpeg. Here’s how you can do it: subprocess.run(['ffmpeg', '-loop', '1', '-i', 'flash_card.png', '-i', 'combined.wav', '-shortest', '-c:v', 'libx264', '-tune', 'stillimage', '-c:a', 'aac', '-b:a', '192k', '-pix_fmt', 'yuv420p', '-vf', 'scale=1280:720', output_file]) print( f'output{i}.mp4') # Add the output file to the list video_files.append(output_file) # Clean up os.remove('combined.wav') os.remove('english.wav') os.remove('french.wav') os.remove('flash_card.png') # Concatenate the videos # Write the names of the video files to a text file with open('video_files.txt', 'w') as f: for video_file in video_files: f.write(f"file '{video_file}'\n") # Use the text file with ffmpeg subprocess.run(['ffmpeg', '-f', 'concat', '-safe', '0', '-i', 'video_files.txt', 'final_output.mp4']) #subprocess.run(['ffmpeg', '-f', 'concat', '-i', ' '.join(video_files), 'final_output.mp4']) # Clean up #for file in video_files: # os.remove(file)