Created
February 28, 2024 13:13
-
-
Save alikiki/35a58c347b56c6a99a6102a582cd8e41 to your computer and use it in GitHub Desktop.
whisper_for_me
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
local hs = hs | |
local isRecording = false | |
local audioFilePath = os.getenv("HOME") .. "/Documents/transcription_audio.mp3" | |
function fileExists(filePath) | |
local file = io.open(filePath, "r") | |
if file then | |
file:close() | |
return true | |
else | |
return false | |
end | |
end | |
function isProcessRunning(processName) | |
local command = "pgrep " .. processName | |
local output = hs.execute(command) | |
return output ~= "" -- If output is not empty, process is running | |
end | |
function printInputDevices() | |
local printingCommand = "ffmpeg -f avfoundation -list_devices true -i ~/Desktop/nothing.mp3 > ~/Desktop/debugging_hammerspoon.txt 2>&1" | |
local success, output, exitType, rc = hs.execute(printingCommand, true) | |
end | |
function startRecording() | |
local recordingCommand = "ffmpeg -f avfoundation -i \":MacBook\" -acodec libmp3lame " .. audioFilePath .. " > /dev/null &" | |
hs.console.printStyledtext("Recording command: " .. recordingCommand) | |
-- Execute the command | |
local success, output, exitType, rc = hs.execute(recordingCommand, true) | |
-- Check the execution status | |
if success then | |
isRecording = true | |
hs.alert.show("Recording Started") | |
else | |
hs.console.printStyledtext("Failed to start recording: " .. output) | |
hs.alert.show("Failed to start recording") | |
end | |
end | |
function stopRecordingAndTranscribe() | |
isRecording = false | |
if isProcessRunning("ffmpeg") then | |
hs.console.printStyledtext("ffmpeg is currently running") | |
else | |
hs.console.printStyledtext("ffmpeg not running") | |
hs.alert.show("FFmpeg not running") | |
return | |
end | |
hs.console.printStyledtext("Stopping recording") | |
hs.execute("killall ffmpeg", true) | |
isRecording = false | |
hs.alert.show("Recording Stopped") | |
if not fileExists(audioFilePath) then | |
hs.alert.show("Transcription failed.") | |
return | |
end | |
local command = "curl https://api.openai.com/v1/audio/transcriptions -H \"Authorization: [YOUR API KEY HERE]\" -H \"Content-Type: multipart/form-data\" -F \"file=@" .. audioFilePath .. "\" -F model=\"whisper-1\" -F prompt=\"Hello. \"" | |
hs.console.printStyledtext(command) | |
local output, status, type, rc = hs.execute(command) | |
hs.console.printStyledtext("Output: " .. output) | |
hs.console.printStyledtext("Status: " .. tostring(status)) | |
hs.console.printStyledtext("Type: " .. type) | |
hs.console.printStyledtext("Return Code: " .. rc) | |
if status then | |
local response = hs.json.decode(output) | |
if response and response.text then | |
hs.pasteboard.setContents(response.text) | |
hs.alert.show("Transcription Copied to Clipboard") | |
else | |
hs.alert.show("Failed to parse response") | |
end | |
else | |
hs.alert.show("API request failed") | |
end | |
-- cleanup | |
local cleanup_command = "rm " .. audioFilePath | |
hs.console.printStyledtext(cleanup_command) | |
hs.execute(cleanup_command) | |
end | |
hs.hotkey.bind({"alt"}, "R", function() | |
hs.console.printStyledtext("Status: " .. tostring(isRecording)) | |
if not isRecording then | |
startRecording() | |
else | |
stopRecordingAndTranscribe() | |
end | |
end) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment