Created
November 23, 2023 20:07
-
-
Save tiriana/e8d2832093de2cbd19c006e37b2d9c1e to your computer and use it in GitHub Desktop.
Synthetic Video Generator Script - A simple and versatile bash script to generate synthetic video files of a specified duration using ffmpeg. Ideal for creating test videos with custom lengths, the script features optional parameters for setting the video's duration and naming the output file. The generated videos come with standard test pattern…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Synthetic Video Generator Script | |
# ============================================================================== | |
# This bash script generates a synthetic video file of a specified duration using ffmpeg. | |
# It's designed to be simple and flexible, allowing users to quickly create video files for testing or other purposes. | |
# | |
# Features: | |
# 1. Optional duration parameter: Specify the length of the video in seconds. | |
# - If not provided, defaults to 60 seconds. | |
# 2. Optional output filename: Define the name of the output video file. | |
# - If not provided, defaults to 'output-<duration>-seconds.mp4'. | |
# 3. Uses ffmpeg to generate a synthetic video with standard test patterns and silent audio. | |
# | |
# Usage: | |
# ./generate_synthetic_video.sh [duration_in_seconds] [output_filename] | |
# Example: ./generate_synthetic_video.sh 120 custom.mp4 | |
# - This generates a 2-minute video file named custom.mp4. | |
# | |
# Requirements: | |
# - ffmpeg must be installed and accessible in the system's PATH. | |
# ============================================================================== | |
# Usage function | |
usage() { | |
echo "Usage: $0 [duration_in_seconds] [output_filename]" | |
echo "Arguments:" | |
echo " duration_in_seconds (Optional) Duration of the video in seconds. Default is 60 seconds." | |
echo " output_filename (Optional) Name of the output video file. Default is 'output-<duration>-seconds.mp4'" | |
echo | |
echo "Example:" | |
echo " $0 120 # Generates a 2-minute video file named output-120-seconds.mp4" | |
echo " $0 120 custom.mp4 # Generates a 2-minute video file named custom.mp4" | |
} | |
# Assign the arguments to variables with default values | |
DURATION=${1:-60} | |
OUTPUT_FILE=${2:-"output-${DURATION}-seconds.mp4"} | |
# Constants | |
AUDIO_BITRATE=128000 # Standard audio bitrate (128kbps) | |
VIDEO_BITRATE=1000000 # Example constant video bitrate (1000kbps) | |
echo "Audio bitrate (bits/s): $AUDIO_BITRATE" | |
echo "Video bitrate (bits/s): $VIDEO_BITRATE" | |
echo "Duration (seconds): $DURATION" | |
# Generate the video with ffmpeg | |
ffmpeg -f lavfi -i testsrc=size=1280x720:rate=30 -f lavfi -i anullsrc=r=44100:cl=stereo -c:v libx264 -b:v $(echo "$VIDEO_BITRATE/1000" | bc)k -t $DURATION -c:a aac -b:a $(echo "$AUDIO_BITRATE/1000" | bc)k -y $OUTPUT_FILE | |
echo "Generated file: $OUTPUT_FILE" | |
echo "File size: $(stat -c%s "$OUTPUT_FILE") bytes" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment