billing.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package gemini
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // ParseVeoDurationSeconds extracts durationSeconds from metadata.
  7. // Returns 8 (Veo default) when not specified or invalid.
  8. func ParseVeoDurationSeconds(metadata map[string]any) int {
  9. if metadata == nil {
  10. return 8
  11. }
  12. v, ok := metadata["durationSeconds"]
  13. if !ok {
  14. return 8
  15. }
  16. switch n := v.(type) {
  17. case float64:
  18. if int(n) > 0 {
  19. return int(n)
  20. }
  21. case int:
  22. if n > 0 {
  23. return n
  24. }
  25. }
  26. return 8
  27. }
  28. // ParseVeoResolution extracts resolution from metadata.
  29. // Returns "720p" when not specified.
  30. func ParseVeoResolution(metadata map[string]any) string {
  31. if metadata == nil {
  32. return "720p"
  33. }
  34. v, ok := metadata["resolution"]
  35. if !ok {
  36. return "720p"
  37. }
  38. if s, ok := v.(string); ok && s != "" {
  39. return strings.ToLower(s)
  40. }
  41. return "720p"
  42. }
  43. // ResolveVeoDuration returns the effective duration in seconds.
  44. // Priority: metadata["durationSeconds"] > stdDuration > stdSeconds > default (8).
  45. func ResolveVeoDuration(metadata map[string]any, stdDuration int, stdSeconds string) int {
  46. if metadata != nil {
  47. if _, exists := metadata["durationSeconds"]; exists {
  48. if d := ParseVeoDurationSeconds(metadata); d > 0 {
  49. return d
  50. }
  51. }
  52. }
  53. if stdDuration > 0 {
  54. return stdDuration
  55. }
  56. if s, err := strconv.Atoi(stdSeconds); err == nil && s > 0 {
  57. return s
  58. }
  59. return 8
  60. }
  61. // ResolveVeoResolution returns the effective resolution string (lowercase).
  62. // Priority: metadata["resolution"] > SizeToVeoResolution(stdSize) > default ("720p").
  63. func ResolveVeoResolution(metadata map[string]any, stdSize string) string {
  64. if metadata != nil {
  65. if _, exists := metadata["resolution"]; exists {
  66. if r := ParseVeoResolution(metadata); r != "" {
  67. return r
  68. }
  69. }
  70. }
  71. if stdSize != "" {
  72. return SizeToVeoResolution(stdSize)
  73. }
  74. return "720p"
  75. }
  76. // SizeToVeoResolution converts a "WxH" size string to a Veo resolution label.
  77. func SizeToVeoResolution(size string) string {
  78. parts := strings.SplitN(strings.ToLower(size), "x", 2)
  79. if len(parts) != 2 {
  80. return "720p"
  81. }
  82. w, _ := strconv.Atoi(parts[0])
  83. h, _ := strconv.Atoi(parts[1])
  84. maxDim := w
  85. if h > maxDim {
  86. maxDim = h
  87. }
  88. if maxDim >= 3840 {
  89. return "4k"
  90. }
  91. if maxDim >= 1920 {
  92. return "1080p"
  93. }
  94. return "720p"
  95. }
  96. // SizeToVeoAspectRatio converts a "WxH" size string to a Veo aspect ratio.
  97. func SizeToVeoAspectRatio(size string) string {
  98. parts := strings.SplitN(strings.ToLower(size), "x", 2)
  99. if len(parts) != 2 {
  100. return "16:9"
  101. }
  102. w, _ := strconv.Atoi(parts[0])
  103. h, _ := strconv.Atoi(parts[1])
  104. if w <= 0 || h <= 0 {
  105. return "16:9"
  106. }
  107. if h > w {
  108. return "9:16"
  109. }
  110. return "16:9"
  111. }
  112. // VeoResolutionRatio returns the pricing multiplier for the given resolution.
  113. // Standard resolutions (720p, 1080p) return 1.0.
  114. // 4K returns a model-specific multiplier based on Google's official pricing.
  115. func VeoResolutionRatio(modelName, resolution string) float64 {
  116. if resolution != "4k" {
  117. return 1.0
  118. }
  119. // 4K multipliers derived from Vertex AI official pricing (video+audio base):
  120. // veo-3.1-generate: $0.60 / $0.40 = 1.5
  121. // veo-3.1-fast-generate: $0.35 / $0.15 ≈ 2.333
  122. // Veo 3.0 models do not support 4K; return 1.0 as fallback.
  123. if strings.Contains(modelName, "3.1-fast-generate") {
  124. return 2.333333
  125. }
  126. if strings.Contains(modelName, "3.1-generate") || strings.Contains(modelName, "3.1") {
  127. return 1.5
  128. }
  129. return 1.0
  130. }