Hermit11 commited on
Commit
525cc78
·
verified ·
1 Parent(s): a61532e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +59 -37
main.py CHANGED
@@ -22,23 +22,12 @@ logger = logging.getLogger(__name__)
22
  class VideoURLs(BaseModel):
23
  urls: List[str]
24
 
25
- MAX_VIDEOS = 10
26
-
27
- '''
28
- # Vimeo client setup
29
- client = vimeo.VimeoClient(
30
- token='6c0e53613726874a46da7c0f286e27b5',
31
- key='45dc18a3c7bdb1dc6255a49d784398b14b4fd55f',
32
- secret='jVwFHFHWs0pGs0rdlHzCnDvkJFru1IeNACt0ygV9bk3mMU45hzZGvg/I+vTzzzvpU1HakvxdxITzf/gokrq5k8VPwQmGirosmltJZAf9/CIAnPpKw19lGrr13MKXKQ1m'
33
- )
34
- '''
35
-
36
- # Vimeo client setup
37
- client = vimeo.VimeoClient(
38
- token='35b24d71f540bfa24e61d488cf34e457',
39
- key='9efa7b1c1827ce4b1d74330d0a90cf62ff4da8b9',
40
- secret='QIwoszSklYElk2wH5PHqe/EzSDB8HM+gdHBg4iaiDs2SkYa+RS3T4ei/VrPWGMMZt2qGXYIbjxfNlPYOtz5g3Ylsys0VpBuiUjkRhz+oR6yR2epl/MW68V1ZxS/D7EwD'
41
- )
42
 
43
  def download_video(url):
44
  try:
@@ -61,6 +50,8 @@ def combine_videos(urls):
61
  temp_files = []
62
  for url in urls:
63
  if url.strip():
 
 
64
  try:
65
  temp_files.append(download_video(url.strip()))
66
  except Exception as e:
@@ -71,6 +62,8 @@ def combine_videos(urls):
71
  raise HTTPException(status_code=400, detail="No valid videos to combine")
72
 
73
  try:
 
 
74
  captures = [cv2.VideoCapture(file) for file in temp_files]
75
 
76
  fps = captures[0].get(cv2.CAP_PROP_FPS)
@@ -102,26 +95,55 @@ def combine_videos(urls):
102
 
103
  def upload_to_vimeo(video_path):
104
  try:
105
- # Upload the video to Vimeo
106
- uri = client.upload(video_path, data={
107
- 'name': 'Combined Video',
108
- 'description': 'This video was combined using our app.'
109
- })
110
-
111
- # Get the Vimeo URL
112
- response = client.get(uri + '?fields=link').json()
113
- vimeo_url = response['link']
114
-
115
- return vimeo_url
116
- except vimeo.exceptions.VideoUploadFailure as e:
117
- logger.error(f"Video upload failed: {str(e)}")
118
- raise HTTPException(status_code=500, detail=f"Failed to upload to Vimeo: {str(e)}")
119
- except vimeo.exceptions.APIRateLimitExceededFailure:
120
- logger.error("API rate limit exceeded")
121
- raise HTTPException(status_code=429, detail="API rate limit exceeded. Please try again later.")
122
- except Exception as e:
123
- logger.error(f"Unexpected error during Vimeo upload: {str(e)}")
124
- raise HTTPException(status_code=500, detail=f"Failed to upload to Vimeo: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  @app.post("/combine_videos")
127
  async def process_urls(video_urls: VideoURLs):
 
22
  class VideoURLs(BaseModel):
23
  urls: List[str]
24
 
25
+ MAX_VIDEOS = 1010
26
+
27
+ # Vimeo API credentials
28
+ VIMEO_ACCESS_TOKEN = "35b24d71f540bfa24e61d488cf34e457" # Replace with your Vimeo token
29
+ VIMEO_UPLOAD_URL = "https://api.vimeo.com/me/videos"
30
+
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  def download_video(url):
33
  try:
 
50
  temp_files = []
51
  for url in urls:
52
  if url.strip():
53
+ # Check if it's a newline character
54
+
55
  try:
56
  temp_files.append(download_video(url.strip()))
57
  except Exception as e:
 
62
  raise HTTPException(status_code=400, detail="No valid videos to combine")
63
 
64
  try:
65
+ # Trim video to 400 milliseconds for newline characters
66
+
67
  captures = [cv2.VideoCapture(file) for file in temp_files]
68
 
69
  fps = captures[0].get(cv2.CAP_PROP_FPS)
 
95
 
96
  def upload_to_vimeo(video_path):
97
  try:
98
+ # Step 1: Request an upload link (initialize the upload)
99
+ headers = {
100
+ 'Authorization': f'Bearer {VIMEO_ACCESS_TOKEN}',
101
+ 'Content-Type': 'application/json',
102
+ 'Accept': 'application/vnd.vimeo.*+json;version=3.4'
103
+ }
104
+
105
+ # Video metadata and upload initialization
106
+ upload_data = {
107
+ 'upload': {
108
+ 'approach': 'tus', # Use the 'tus' approach for large file uploads
109
+ 'size': os.path.getsize(video_path)
110
+ },
111
+ 'name': 'Uploaded Video',
112
+ 'description': 'This video was uploaded using the Vimeo Upload API.'
113
+ }
114
+
115
+ # Send request to create an upload ticket
116
+ response = requests.post(VIMEO_UPLOAD_URL, json=upload_data, headers=headers)
117
+ response.raise_for_status()
118
+ vimeo_data = response.json()
119
+
120
+ # Extract the upload link
121
+ upload_link = vimeo_data['upload']['upload_link']
122
+ video_uri = vimeo_data['uri'] # Used to get the video link after upload
123
+
124
+ # Step 2: Upload the video file using the provided upload link
125
+ tus_headers = {
126
+ 'Tus-Resumable': '1.0.0',
127
+ 'Upload-Offset': '0',
128
+ 'Content-Type': 'application/offset+octet-stream',
129
+ 'Authorization': f'Bearer {VIMEO_ACCESS_TOKEN}'
130
+ }
131
+
132
+ with open(video_path, 'rb') as video_file:
133
+ tus_response = requests.patch(upload_link, headers=tus_headers, data=video_file)
134
+ tus_response.raise_for_status()
135
+
136
+ # Step 3: Confirm the upload and retrieve the Vimeo video link
137
+ video_response = requests.get(f"https://api.vimeo.com{video_uri}?fields=link", headers=headers)
138
+ video_response.raise_for_status()
139
+ video_link = video_response.json()['link']
140
+
141
+ print(f"Video uploaded successfully: {video_link}")
142
+ return video_link
143
+
144
+ except requests.RequestException as e:
145
+ print(f"Error uploading video to Vimeo: {str(e)}")
146
+ raise
147
 
148
  @app.post("/combine_videos")
149
  async def process_urls(video_urls: VideoURLs):