Whisper Api를 이용해서 자막을 추출하는 워드프레스 페이지를 만드는 방법
이 문서는 Whisper API를 활용하여 워드프레스 페이지에 음성 파일을 업로드하고, 자막 파일을 추출하여 다운로드 받는 방법을 안내합니다.
요구 사항
이 기능을 구현하기 위해서는 다음과 같은 요구 사항이 필요합니다.
- Whisper API 계정
- 워드프레스 사이트
- 서버 측에서 PHP를 실행할 수 있는 환경
Whisper API 설정
- Whisper API 홈페이지에 접속하여 계정을 생성합니다.
- 생성한 계정으로 로그인한 후, "Developer Console"을 클릭합니다.
- "API Keys" 탭에서 "New API Key" 버튼을 클릭하여 API Key를 생성합니다.
- 생성된 API Key를 복사하여 안전한 장소에 보관합니다.
워드프레스 페이지 설정
- 워드프레스 사이트에 로그인한 후, "Plugins" 메뉴에서 "Add new" 버튼을 클릭합니다.
- "Search plugins" 입력란에 "Whisper"를 입력하여 Whisper 플러그인을 검색합니다.
- 검색 결과에서 Whisper 플러그인을 설치하고 활성화합니다.
- "Settings" 메뉴에서 "Whisper Settings"를 클릭합니다.
- "API Key" 입력란에 이전에 생성한 Whisper API Key를 입력합니다.
- "Save Changes" 버튼을 클릭하여 설정을 저장합니다.
코드 구현
다음은 Whisper API를 활용하여 자막을 추출하는 코드입니다.
// 자막 추출을 위한 Whisper API 호출 함수
function whisper_api_request($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// 음성 파일 업로드 및 자막 추출 함수
function upload_and_extract_subtitles() {
// 파일 업로드 처리
if(isset($_FILES['audio_file'])) {
$file_name = $_FILES['audio_file']['name'];
$file_tmp = $_FILES['audio_file']['tmp_name'];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$extensions = array("mp3","wav","ogg");
if(in_array($file_ext, $extensions) === false) {
echo "extension not allowed, please choose an audio file.";
return;
}
$file_name = uniqid() . '.' . $file_ext;
move_uploaded_file($file_tmp, "uploads/" . $file_name);
}
else {
echo "audio file not found.";
return;
}
// Whisper API 호출을 위한 URL 생성
$whisper_api_key = get_option('whisper_api_key');
$url = "<https://api.whisper.ai/extract_subtitle?api_key=$whisper_api_key&audio_file=https://example.com/uploads/$file_name>";
// Whisper API 호출 및 자막 추출 결과 처리
$response = whisper_api_request($url);
$response = json_decode($response, true);
if(!isset($response['error'])) {
$subtitles = $response['subtitles'];
$file = fopen("uploads/" . pathinfo($file_name, PATHINFO_FILENAME) . ".srt","w");
fwrite($file, $subtitles);
fclose($file);
echo "<a href=\"<https://example.com/uploads/>" . pathinfo($file_name, PATHINFO_FILENAME) . ".srt\">Download subtitles</a>";
}
else {
echo "error: " . $response['error'];
}
}
// 워드프레스 페이지에 파일 업로드 폼 및 자막 추출 버튼 출력
function whisper_upload_form() {
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="audio_file" />
<input type="submit" name="submit" value="Extract subtitles" />
</form>
<?php
if(isset($_POST['submit'])) {
upload_and_extract_subtitles();
}
}
add_shortcode('whisper_upload_form', 'whisper_upload_form');
위 코드를 사용하여 워드프레스 페이지에 자막 추출 기능을 구현할 수 있습니다.
사용 방법
- 워드프레스 페이지에
[whisper_upload_form]을 입력하여 파일 업로드 폼과 자막 추출 버튼을 출력합니다. - "Choose File" 버튼을 클릭하여 추출할 음성 파일을 선택합니다.
- "Extract subtitles" 버튼을 클릭하여 자막 추출을 시작합니다.
- 자막 추출이 완료되면 자막 파일을 다운로드할 수 있는 링크가 출력됩니다.




