1
3

Pythonでmp4の動画から音声のみを抽出したmp3ファイルを作成する

Posted at

前提

  • mp4の動画から音声のみを抽出したmp3ファイルを作成したい
  • 今回はffmpeg-pythonライブラリを使います
  • mp4ファイルはBytesIOの形式で渡されるものとします

実装

import tempfile
import ffmpeg
from io imoprt BytesIO

def convert_to_mp3_from_mp4(file: BytesIO)

    # ffmpegのinputはファイル形式しか受け付けないためtempfileで一時ファイルを作成します
    with tempfile.NamedTemporaryFile() as temp_file:
        temp_file.write(file.getvalue())
        # mp3に変換します
        (
            ffmpeg
            .input(temp_file.name)
            .output("output.mp3")
            .run()
        )

1
3
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
1
3