Class: Lipdub::Resources::Audios

Inherits:
Base
  • Object
show all
Defined in:
lib/lipdub/resources/audios.rb

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Lipdub::Resources::Base

Instance Method Details

#failure(audio_id) ⇒ Hash

Mark audio upload as failed

Examples:

client.audios.failure("audio_123")

Parameters:

  • audio_id (String)

    Unique identifier of the audio file

Returns:

  • (Hash)

    Response (typically empty)



123
124
125
# File 'lib/lipdub/resources/audios.rb', line 123

def failure(audio_id)
  post("/v1/audio/failure/#{audio_id}")
end

#list(page: 1, page_size: 10) ⇒ Hash

List all audio files

Examples:

audios = client.audios.list(page: 1, page_size: 20)

Parameters:

  • page (Integer) (defaults to: 1)

    Page number (defaults to 1)

  • page_size (Integer) (defaults to: 10)

    Number of items per page (defaults to 10)

Returns:

  • (Hash)

    Response containing list of audio files



146
147
148
149
150
151
152
# File 'lib/lipdub/resources/audios.rb', line 146

def list(page: 1, page_size: 10)
  params = {
    page: page,
    page_size: page_size
  }
  get("/v1/audio", params)
end

#status(audio_id) ⇒ Hash

Get audio processing status

Examples:

status = client.audios.status("audio_123")

Parameters:

  • audio_id (String)

    Unique identifier of the audio file

Returns:

  • (Hash)

    Response containing audio status information



134
135
136
# File 'lib/lipdub/resources/audios.rb', line 134

def status(audio_id)
  get("/v1/audio/status/#{audio_id}")
end

#success(audio_id) ⇒ Hash

Mark audio upload as successful

Examples:

client.audios.success("audio_123")

Parameters:

  • audio_id (String)

    Unique identifier of the audio file

Returns:

  • (Hash)

    Response (typically empty)



112
113
114
# File 'lib/lipdub/resources/audios.rb', line 112

def success(audio_id)
  post("/v1/audio/success/#{audio_id}")
end

#upload(size_bytes:, file_name:, content_type:, audio_source_url: nil) ⇒ Hash

Initiate audio upload process

Examples:

response = client.audios.upload(
  size_bytes: 5242880,
  file_name: "voiceover.mp3",
  content_type: "audio/mpeg"
)
# => {
#   "data" => {
#     "audio_id" => "audio_123",
#     "upload_url" => "https://storage.lipdub.ai/upload/audio_123?token=xyz",
#     "success_url" => "https://api.lipdub.ai/v1/audio/success/audio_123",
#     "failure_url" => "https://api.lipdub.ai/v1/audio/failure/audio_123"
#   }
# }

Parameters:

  • size_bytes (Integer)

    Size of the audio file in bytes (1 to 104857600)

  • file_name (String)

    Name of the audio file with extension

  • content_type (String)

    MIME type of the audio file (audio/mpeg, audio/wav, audio/mp4)

  • audio_source_url (String, nil) (defaults to: nil)

    Optional URL of the audio source file

Returns:

  • (Hash)

    Response containing audio_id, upload_url, success_url, and failure_url



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lipdub/resources/audios.rb', line 28

def upload(size_bytes:, file_name:, content_type:, audio_source_url: nil)
  validate_audio_params!(size_bytes, content_type)
  
  body = {
    size_bytes: size_bytes,
    file_name: file_name,
    content_type: content_type
  }
  body[:audio_source_url] = audio_source_url if audio_source_url

  post("/v1/audio", body)
end

#upload_complete(file_path, content_type: nil) ⇒ Hash

Complete audio upload process with a file path

Examples:

response = client.audios.upload_complete("path/to/audio.mp3")
# This method handles the entire upload flow:
# 1. Initiates upload
# 2. Uploads the file
# 3. Calls success callback

Parameters:

  • file_path (String)

    Path to the audio file

  • content_type (String, nil) (defaults to: nil)

    MIME type of the audio file (auto-detected if nil)

Returns:

  • (Hash)

    Response after successful upload

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/lipdub/resources/audios.rb', line 67

def upload_complete(file_path, content_type: nil)
  raise ArgumentError, "File does not exist: #{file_path}" unless File.exist?(file_path)

  file_content = File.read(file_path)
  file_name = File.basename(file_path)
  content_type ||= detect_content_type(file_path)
  size_bytes = File.size(file_path)

  # Step 1: Initiate upload
  upload_response = upload(
    size_bytes: size_bytes,
    file_name: file_name,
    content_type: content_type
  )

  audio_id = upload_response.dig("data", "audio_id") if upload_response.is_a?(Hash)
  upload_url = upload_response.dig("data", "upload_url") if upload_response.is_a?(Hash)
  
  # Handle case where response might still be a string (fallback)
  if upload_response.is_a?(String)
    parsed = JSON.parse(upload_response)
    audio_id = parsed.dig("data", "audio_id")
    upload_url = parsed.dig("data", "upload_url")
  end
  
  begin
    # Step 2: Upload file
    upload_file(upload_url, file_content, content_type)
    
    # Step 3: Mark as successful
    success(audio_id)
  rescue => e
    # Step 3 (alternative): Mark as failed
    failure(audio_id)
    raise e
  end
end

#upload_file(upload_url, file_content, content_type) ⇒ Hash

Upload audio file to the provided upload URL

Examples:

file_content = File.read("voiceover.mp3")
client.audios.upload_file(upload_url, file_content, "audio/mpeg")

Parameters:

  • upload_url (String)

    The upload URL received from the upload method

  • file_content (String, IO)

    The audio file content to upload

  • content_type (String)

    MIME type of the audio file

Returns:

  • (Hash)

    Response from the upload



51
52
53
# File 'lib/lipdub/resources/audios.rb', line 51

def upload_file(upload_url, file_content, content_type)
  put_file(upload_url, file_content, content_type)
end