Last active
May 4, 2023 21:23
-
-
Save marcushellberg/8bfb07f7ebaabcef452281ee5771d4ea to your computer and use it in GitHub Desktop.
Generate SRT from video with OpenAI whisper
This file contains hidden or 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
const ffmpeg = require("fluent-ffmpeg"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const yargs = require("yargs/yargs"); | |
const { hideBin } = require("yargs/helpers"); | |
const argv = yargs(hideBin(process.argv)).argv; | |
const { Configuration, OpenAIApi } = require("openai"); | |
// Set OpenAI API key | |
const configuration = new Configuration({ | |
apiKey: process.env.OPENAI_API_KEY, | |
}); | |
const openai = new OpenAIApi(configuration); | |
// Function to convert MP4 to MP3 | |
function convertToMp3(inputFile, outputFile) { | |
return new Promise((resolve, reject) => { | |
ffmpeg(inputFile) | |
.noVideo() | |
.toFormat("mp3") | |
.output(outputFile) | |
.on("end", resolve) | |
.on("error", reject) | |
.run(); | |
}); | |
} | |
// Function to transcribe audio to SRT | |
async function transcribeToSrt(inputFile) { | |
const resp = await openai.createTranscription( | |
fs.createReadStream(inputFile), | |
"whisper-1", | |
undefined, | |
"srt", | |
0, | |
"en" | |
); | |
return resp; | |
} | |
// Main function to process the video file | |
async function processVideoFile(videoFilePath) { | |
const videoDir = path.dirname(videoFilePath); | |
const videoBaseName = path.basename( | |
videoFilePath, | |
path.extname(videoFilePath) | |
); | |
const audioFilePath = path.join(videoDir, `${videoBaseName}.mp3`); | |
try { | |
await convertToMp3(videoFilePath, audioFilePath); | |
console.log(`Audio extracted to ${audioFilePath}`); | |
const resp = await transcribeToSrt(audioFilePath); | |
fs.writeFileSync(`${videoDir}/${videoBaseName}.srt`, resp.data); | |
console.log(`SRT file created at ${videoDir}/${videoBaseName}.srt`); | |
} catch (err) { | |
console.error(`Failed to process video file: ${err}`); | |
} | |
} | |
// Get the video file path from command line argument | |
const videoFilePath = argv._[0]; | |
if (!videoFilePath) { | |
console.error("Please provide a video file path as a command line argument"); | |
process.exit(1); | |
} | |
processVideoFile(videoFilePath); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment