Class: Lipdub::Resources::Videos

Inherits:
Base
  • Object
show all
Defined in:
lib/lipdub/resources/videos.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(video_id) ⇒ Hash

Mark video upload as failed

Examples:

client.videos.failure("video_123")

Parameters:

  • video_id (String)

    Unique identifier of the video file

Returns:

  • (Hash)

    Response (typically empty)



127
128
129
# File 'lib/lipdub/resources/videos.rb', line 127

def failure(video_id)
  post("/v1/video/failure/#{video_id}")
end

#status(video_id) ⇒ Hash

Get video processing status

Examples:

status = client.videos.status("video_123")

Parameters:

  • video_id (String)

    Unique identifier of the video file

Returns:

  • (Hash)

    Response containing video status information



138
139
140
# File 'lib/lipdub/resources/videos.rb', line 138

def status(video_id)
  get("/v1/video/status/#{video_id}")
end

#success(video_id) ⇒ Hash

Mark video upload as successful

Examples:

response = client.videos.success("video_123")
# => {
#   "data" => {
#     "shot_id" => 123,
#     "asset_type" => "dubbing-video"
#   }
# }

Parameters:

  • video_id (String)

    Unique identifier of the video file

Returns:

  • (Hash)

    Response containing shot_id and asset_type



116
117
118
# File 'lib/lipdub/resources/videos.rb', line 116

def success(video_id)
  post("/v1/video/success/#{video_id}")
end

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

Initiate video upload process

Examples:

response = client.videos.upload(
  size_bytes: 52428800,
  file_name: "sample.mp4",
  content_type: "video/mp4"
)
# => {
#   "data" => {
#     "video_id" => "video_123",
#     "upload_url" => "https://storage.lipdub.ai/upload/video_123?token=xyz",
#     "success_url" => "https://api.lipdub.ai/v1/video/success/video_123",
#     "failure_url" => "https://api.lipdub.ai/v1/video/failure/video_123"
#   }
# }

Parameters:

  • size_bytes (Integer)

    Size of the video file in bytes

  • file_name (String)

    Name of the video file with extension

  • content_type (String)

    MIME type of the video file

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

    Optional URL of the video source file

Returns:

  • (Hash)

    Response containing video_id, upload_url, success_url, and failure_url



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

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

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

#upload_complete(file_path, content_type: nil) ⇒ Hash

Complete video upload process with a file path

Examples:

response = client.videos.upload_complete("path/to/video.mp4")
# 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 video file

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

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

Returns:

  • (Hash)

    Response containing shot_id and asset_type after successful upload

Raises:

  • (ArgumentError)


65
66
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
# File 'lib/lipdub/resources/videos.rb', line 65

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
  )

  video_id = upload_response.dig("data", "video_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)
    video_id = parsed.dig("data", "video_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(video_id)
  rescue => e
    # Step 3 (alternative): Mark as failed
    failure(video_id)
    raise e
  end
end

#upload_file(upload_url, file_content, content_type) ⇒ Hash

Upload video file to the provided upload URL

Examples:

file_content = File.read("sample.mp4")
client.videos.upload_file(upload_url, file_content, "video/mp4")

Parameters:

  • upload_url (String)

    The upload URL received from the upload method

  • file_content (String, IO)

    The video file content to upload

  • content_type (String)

    MIME type of the video file

Returns:

  • (Hash)

    Response from the upload



49
50
51
# File 'lib/lipdub/resources/videos.rb', line 49

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