Skip to content

Instantly share code, notes, and snippets.

@knee-cola
Created August 19, 2024 12:32
Show Gist options
  • Save knee-cola/fa056f31916f982c03be6a46ea51db9a to your computer and use it in GitHub Desktop.
Save knee-cola/fa056f31916f982c03be6a46ea51db9a to your computer and use it in GitHub Desktop.
#!/bin/bash
# Converts a SBV (YouTube) Subtitles to CSV format,
# so that they can be imported to Spreadsheet for editing
# Check if input file is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 input_file.sbv"
exit 1
fi
input_file="$1"
output_file="${input_file%.sbv}.csv"
# Initialize variables
timestamp=""
text=""
# Start writing the CSV output
echo "Timestamp,Text" > "$output_file"
# Read the input SBV file line by line
while IFS= read -r line; do
# If the line is a timestamp
if [[ $line =~ ^[0-9]+:[0-9]+:[0-9]+\.[0-9]+,[0-9]+:[0-9]+:[0-9]+\.[0-9]+$ ]]; then
# If there is previous text, write it to the CSV
if [[ -n $timestamp && -n $text ]]; then
# Escape any quotes in the text and replace newlines with <br/>
text=$(echo "$text" | sed ':a;N;$!ba;s/\n/<br\/>/g' | sed 's/"/""/g')
echo "\"$timestamp\",\"$text\"" >> "$output_file"
fi
# Reset for new block
timestamp="$line"
text=""
# If the line is text
elif [[ -n $line ]]; then
# Add text to the current block
if [[ -n $text ]]; then
text="$text<br/>$line"
else
text="$line"
fi
fi
done < "$input_file"
# Write the last block if it exists
if [[ -n $timestamp && -n $text ]]; then
# Escape any quotes in the text and replace newlines with <br/>
text=$(echo "$text" | sed ':a;N;$!ba;s/\n/<br\/>/g' | sed 's/"/""/g')
echo "\"$timestamp\",\"$text\"" >> "$output_file"
fi
echo "Conversion completed. CSV file saved as $output_file."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment