File size: 1,792 Bytes
7cc81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import shutil
import subprocess

import pytest

from app.models.media import InputMedia, MediaSource
from app.operations.convert import convert_audio
from app.services.ffmpeg_service import FFmpegService


@pytest.mark.skipif(shutil.which("ffmpeg") is None, reason="ffmpeg is not installed")
async def test_ffmpeg_audio_conversion(settings, tmp_path) -> None:
    source = tmp_path / "tone.wav"
    subprocess.run(
        [
            "ffmpeg",
            "-hide_banner",
            "-loglevel",
            "error",
            "-f",
            "lavfi",
            "-i",
            "sine=frequency=440:duration=0.2",
            "-y",
            str(source),
        ],
        check=True,
    )
    media = InputMedia(
        source=MediaSource.MULTIPART,
        filename=source.name,
        mime_type="audio/wav",
        temp_path=source,
        size=source.stat().st_size,
    )
    result = await convert_audio(
        FFmpegService(settings), [media], {"format": "mp3"}, tmp_path / "out"
    )
    assert result.path is not None
    assert result.path.is_file()
    assert result.path.stat().st_size > 0


async def test_ffmpeg_codec_listing_is_structured(settings, monkeypatch) -> None:
    service = FFmpegService(settings)

    async def fake_capture(*args, **kwargs) -> str:
        return """Codecs:
 D..... = Decoding supported
 .E.... = Encoding supported
 -------
 DEV.LS h264 H.264 / AVC / MPEG-4 AVC
 DEA.L. aac AAC (Advanced Audio Coding)
"""

    monkeypatch.setattr(service, "_capture", fake_capture)

    codecs = await service.codecs()

    assert [codec["name"] for codec in codecs] == ["h264", "aac"]
    assert codecs[0]["decode"] is True
    assert codecs[0]["encode"] is True
    assert codecs[0]["type"] == "video"