0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

whisper-1

Last updated at Posted at 2023-11-21

このプログラムは、MP3ファイルをテキストに変換するためのものです。以下がその処理内容の概要です:

  1. OpenAI APIキーの取得:プログラムは、環境変数OPENAI_API_KEYからOpenAIのAPIキーを取得します。

  2. 最新のMP3ファイルの検索:指定されたディレクトリから、最新のMP3ファイルを探し出します。

  3. MP3ファイルの変換:見つかったMP3ファイルをOpenAIのwhisper-1エンジンを使用してテキストに変換します。この変換は、MP3ファイルの音声内容をテキスト形式に書き出すことを意味します。

  4. 変換結果の保存:変換されたテキストは、二つの場所に保存されます。一つは同じディレクトリに.txt形式で保存され、もう一つは/home/user/Dropbox/wp-uploads/ディレクトリにsplitter-input.txtとして保存されます。

  5. エラーハンドリング:変換プロセス中に何らかのエラーが発生した場合、そのエラーメッセージが表示されます。

このプログラムはコマンドラインから実行され、/home/user/MP3Sourceディレクトリ内の最新のMP3ファイルを自動的に探し出し、変換してテキストとして保存するように設計されています。

import os
import openai

# OpenAI APIキーを環境変数から取得
openai.api_key = os.getenv('OPENAI_API_KEY')

# 最新のMP3ファイルを取得する関数
def get_latest_mp3_file(mp3_dir):
    print("Searching for the latest MP3 file...")
    mp3_files = [f for f in os.listdir(mp3_dir) if f.lower().endswith('.mp3')]
    if mp3_files:
        mp3_files.sort(key=lambda x: os.path.getmtime(os.path.join(mp3_dir, x)), reverse=True)
        print(f"Found latest MP3 file: {mp3_files[0]}")
        return mp3_files[0]
    else:
        print("No MP3 files found.")
        return None

# MP3ファイルをテキストに変換する関数
def convert_mp3_to_text(mp3_dir, output_dir):
    latest_mp3_filename = get_latest_mp3_file(mp3_dir)

    if latest_mp3_filename:
        mp3_file_path = os.path.join(mp3_dir, latest_mp3_filename)
        output_text_file = os.path.join(output_dir, os.path.splitext(latest_mp3_filename)[0] + '.txt')

        print("Attempting to convert MP3 to text using OpenAI Whisper engine...")
        try:
            with open(mp3_file_path, "rb") as audio_file:
                transcript = openai.Audio.transcribe("whisper-1", audio_file)

                if 'text' in transcript:
                    text_content = transcript["text"]
                else:
                    raise ValueError("Unexpected response structure from OpenAI API.")

                with open(output_text_file, 'w') as txt_file:
                    txt_file.write(text_content)

                # 新しいファイルにも内容を出力
                with open(os.path.join("/home/user/MP3Source/", "splitter-input.txt"), 'w') as whisper_file:
                    whisper_file.write(text_content)

                print(f'Successfully converted MP3 to text and saved to {output_text_file}')

        except Exception as e:
            print(f'Error during conversion: {str(e)}')
            if 'transcript' in locals():
                print(f"OpenAI API Response: {transcript}")

    else:
        print('Conversion aborted due to absence of MP3 files.')

if __name__ == '__main__':
    mp3_dir = '/home/user/MP3Source'
    output_dir = '/home/user/MP3Source'

    print("Checking output directory...")
    if not os.path.exists(output_dir):
        print(f"Output directory {output_dir} does not exist. Creating now...")
        os.makedirs(output_dir)

    convert_mp3_to_text(mp3_dir, output_dir)

改善版を作りました。OpenAI Whisper APIからの応答に segments キーが含まれていないことを示しています。うまくいかないです。

import os
import openai

# OpenAI APIキーを環境変数から取得
openai.api_key = os.getenv('OPENAI_API_KEY')

# 最新のMP3ファイルを取得する関数
def get_latest_mp3_file(mp3_dir):
    print("Searching for the latest MP3 file...")
    mp3_files = [f for f in os.listdir(mp3_dir) if f.lower().endswith('.mp3')]
    if mp3_files:
        mp3_files.sort(key=lambda x: os.path.getmtime(os.path.join(mp3_dir, x)), reverse=True)
        print(f"Found latest MP3 file: {mp3_files[0]}")
        return mp3_files[0]
    else:
        print("No MP3 files found.")
        return None

# SRTファイル形式に変換する関数
def convert_to_srt_format(transcript, output_filename):
    with open(output_filename, 'w') as file:
        for i, segment in enumerate(transcript['segments']):
            start_time = format_timestamp(segment['start_timestamp'])
            end_time = format_timestamp(segment['end_timestamp'])
            text = segment['text']
            file.write(f"{i+1}\n")
            file.write(f"{start_time} --> {end_time}\n")
            file.write(f"{text}\n\n")

def format_timestamp(timestamp):
    hours, remainder = divmod(timestamp, 3600)
    minutes, seconds = divmod(remainder, 60)
    milliseconds = int((seconds - int(seconds)) * 1000)
    return f"{int(hours):02}:{int(minutes):02}:{int(seconds):02},{milliseconds:03}"

# MP3ファイルをテキストとSRTに変換する関数
def convert_mp3_to_text_and_srt(mp3_dir, output_dir):
    latest_mp3_filename = get_latest_mp3_file(mp3_dir)

    if latest_mp3_filename:
        mp3_file_path = os.path.join(mp3_dir, latest_mp3_filename)
        output_text_file = os.path.join(output_dir, os.path.splitext(latest_mp3_filename)[0] + '.txt')
        output_srt_file = os.path.join(output_dir, os.path.splitext(latest_mp3_filename)[0] + '.srt')

        print("Attempting to convert MP3 to text using OpenAI Whisper engine...")
        try:
            with open(mp3_file_path, "rb") as audio_file:
                transcript = openai.Audio.transcribe("whisper-1", audio_file, timestamps=True)

                if 'segments' in transcript:
                    # テキストファイルの保存
                    with open(output_text_file, 'w') as txt_file:
                        for segment in transcript['segments']:
                            txt_file.write(segment['text'] + '\n')

                    # SRTファイルの保存
                    convert_to_srt_format(transcript, output_srt_file)

                    print(f'Successfully converted MP3 to text and saved to {output_text_file}')
                    print(f'SRT file saved as {output_srt_file}')

                else:
                    # segments キーがない場合、通常のテキストファイルのみを保存
                    print("No segments in transcript. Saving only text file.")
                    with open(output_text_file, 'w') as txt_file:
                        if 'text' in transcript:
                            txt_file.write(transcript["text"])

        except Exception as e:
            print(f'Error during conversion: {str(e)}')
            if 'transcript' in locals():
                print(f"OpenAI API Response: {transcript}")

    else:
        print('Conversion aborted due to absence of MP3 files.')

if __name__ == '__main__':
    mp3_dir = '/home/user/MP3Source'
    output_dir = '/home/user/MP3Source'

    print("Checking output directory...")
    if not os.path.exists(output_dir):
        print(f"Output directory {output_dir} does not exist. Creating now...")
        os.makedirs(output_dir)

    convert_mp3_to_text_and_srt(mp3_dir, output_dir)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?