import csv import subprocess import matplotlib.pyplot as plt import os import random import matplotlib.font_manager as fm #cpulimit -e ffmpeg -l 150 # Clean up #os.remove('combined.wav') #os.remove('english.wav') #os.remove('other.wav') #os.remove('flash_card.png') # Create a list to store the names of the video files video_files = [] # Parse the data for i in range(607,6000): try: text_file = f'content_{i+1}.txt' audio_file = f'explain_{i+1}.wav' content="" with open(text_file, 'r') as file: content = file.read() content = content.replace('[hi]','').replace('[/hi]','').replace('\n','.').replace('*','') # 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] # Load a font that supports Chinese characters #font_path = '/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc' # Update this path #prop = fm.FontProperties(fname=font_path) fig, ax = plt.subplots(figsize=(7,5)) # Adjusted for better visibility #fontproperties=prop, ax.text(0, 0, f'{content}', fontsize=13, color='white', 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()) print(f'{i}') #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', audio_file, '-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('other.wav') os.remove('flash_card.png') except: print('oops') # 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']) #ffmpeg -f concat -safe 0 -i video_files.txt final_output.mp4 # Clean up #for file in video_files: # os.remove(file)