Certainly! You can use FFmpeg to create a video from an MP3 file and VTT subtitles. Below is a step-by-step guide to achieve this.
Step 1: Prepare Your Files
Ensure you have the following files in the same directory:
audio.mp3: Your audio file.
subtitles.vtt: Your VTT subtitle file.
Step 2: Create a Black Video
First, you need to create a black video that matches the duration of your MP3 file. You can do this with the following command:
ffmpeg -f lavfi -i color=c=black:s=1280x720:d=$(ffmpeg -i audio.mp3 2>&1 | grep "Duration" | awk '{print $2}' | tr -d ,) -c:v libx264 black_video.mp4
Explanation:
-f lavfi -i color=c=black:s=1280x720:d=$(ffmpeg -i audio.mp3 2>&1 | grep "Duration" | awk '{print $2}' | tr -d ,):
-f lavfi: Specifies that the input is from the Libavfilter framework.
-i color=c=black:s=1280x720:d=$(...): Generates a black video.
c=black: Sets the colour to black.
s=1280x720: Sets the resolution to 1280x720.
d=$(...): Sets the duration to match the MP3 file.
ffmpeg -i audio.mp3 2>&1 | grep "Duration" | awk '{print $2}' | tr -d ,: Extracts the duration of the MP3 file.
-c:v libx264: Specifies the video codec as H.264.
black_video.mp4: The output black video file.
Step 3: Burn Subtitles into the Video
Next, you will burn the VTT subtitles into the black video.
ffmpeg -i black_video.mp4 -i audio.mp3 -vf "subtitles=subtitles.vtt:force_style='Fontsize=24,PrimaryColour=&H00FFFFFF&'" -c:v libx264 -c:a aac -strict experimental -shortest output_video.mp4
Explanation:
-i black_video.mp4: Input the black video.
-i audio.mp3: Input the audio file
-vf "subtitles=subtitles.vtt:force_style='Fontsize=24,PrimaryColour=&H00FFFFFF&'":
-vf: Specifies video filters.
subtitles=subtitles.vtt: Burns the VTT subtitles into the video.
force_style='Fontsize=24,PrimaryColour=&H00FFFFFF&': Sets the subtitle style.
Fontsize=24: Sets the font size to 24.
PrimaryColour=&H00FFFFFF&: Sets the font colour to white.
-c:v libx264: Specifies the video codec as H.264.
-c:a aac: Specifies the audio codec as AAC.
-strict experimental: Allows the use of experimental features.
-shortest: Ensures the output video duration matches the shortest input (in this case, the audio file).
output_video.mp4: The final output video file with audio and burned-in subtitles.
Full Script
Here is the full script for convenience:
# Step 2: Create a black video
ffmpeg -f lavfi -i color=c=black:s=1280x720:d=$(ffmpeg -i audio.mp3 2>&1 | grep "Duration" | awk '{print $2}' | tr -d , ) -c:v libx264 black_video.mp4
# Step 3: Burn subtitles into the video and add audio
ffmpeg -i black_video.mp4 -i audio.mp3 -vf "subtitles=subtitles.vtt:force_style='Fontsize=24,PrimaryColour=&H00FFFFFF&'" -c:v libx264 -c:a aac -strict experimental -shortest output_video.mp4
Notes
- Duration Extraction: The command
ffmpeg -i audio.mp3 2>&1 | grep "Duration" | awk '{print $2}' | tr -d , extracts the duration of the MP3 file. Ensure awk and tr are installed on your system.
- Subtitle Style: You can customise the subtitle style by modifying the
force_style parameter. For example change the font size or colour to your liking.
- Resolution: The resolution
s=1280x720 can be changed to any desired resolution
- Codecs: The video codec
-c:v libx264 and audio codec -c:a aac can be changed to any desired codecs.