-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathllmpeg
executable file
·83 lines (73 loc) · 3.26 KB
/
llmpeg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
# Check if user input is provided
if [ -z "$*" ]; then
echo "Requires a prompt of what you want to do, i.e."
echo "\"llmpeg remove audio from example.mov\""
exit 1
fi
# Capture command-line arguments
user_input="$*"
# Conditional check for API keys
if [ -n "$OPENAI_API_KEY" ]; then
# Store the curl response as a variable for OpenAI API
response=$(curl -s "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "{
\"model\": \"gpt-4o-mini\",
\"messages\": [
{
\"role\": \"system\",
\"content\": \"You write ffmpeg commands based on the description from the user. You should only respond with a command line command for ffmpeg, never any additional text. All responses should be a single line without any line breaks.\"
},
{
\"role\": \"user\",
\"content\": \"$user_input\"
}
]
}")
elif [ -n "$GROQ_API_KEY" ]; then
# Store the curl response as a variable for GROQ API
response=$(curl -s "https://api.groq.com/openai/v1/chat/completions" \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${GROQ_API_KEY}" \
-d "{
\"model\": \"llama-3.3-70b-versatile\",
\"messages\": [
{
\"role\": \"system\",
\"content\": \"You write ffmpeg commands based on the description from the user. You should only respond with a command line command for ffmpeg, never any additional text. Keep the output folder same as the input folder in the generated command. Propose the output file name based on input file name and the modification applied on it. All responses should be a single line without any line breaks.\n\nFor example, if the user asks \\\"convert a video folderX/input.mp4 from MP4 to WebM?\\\", you should respond with: \\\"ffmpeg -i folderX/input.mp4 -c:v libvpx -c:a libvorbis folderX/output_in_webm.webm\\\"\"
},
{
\"role\": \"user\",
\"content\": \"$user_input\"
}
],
\"temperature\": 1,
\"max_tokens\": 1024,
\"top_p\": 1,
\"stream\": false,
\"stop\": null
}")
else
echo "No API key found. Please set OPENAI_API_KEY or GROQ_API_KEY."
exit 1
fi
# Parse the "content" field from the response using grep and sed, ignoring escaped quotes
content=$(echo "$response" | grep -o '"content": *"[^"\\]*\(\\.[^"\\]*\)*"')
# Remove the content prefix
command=$(echo "$content" | sed 's/"content": *"//;s/"$//')
# Remove backslashes from escaped quotes
command=$(echo "$command" | sed 's/\\"/"/g')
# Check if content is empty or null
if [ -z "$content" ]; then
echo "Error: No command generated or API response is empty."
exit 1
fi
# Echo the command
echo "Generated command: $command"
# Prompt the user to run the command
read -p "Press Enter to run the command, or Ctrl+C to cancel..."
# Run the command with additional options
eval "$(echo "$command" | sed 's/ffmpeg/ffmpeg -v quiet -stats/')"