File size: 1,404 Bytes
266909b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import sys
import yt_dlp
import os
from datetime import datetime

from decode_video import decode_video

def youtube_decode(src, dest_folder):
    base_filename = "downloaded_video"
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    output_file = f"{dest_folder}/{base_filename}_{timestamp}.mp4"

    # Create a yt-dlp configuration to download the video
    ydl_opts = {
        'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
        'outtmpl': output_file,
        'noplaylist': True,
        'quiet': True,
        'merge_output_format': 'mp4',  # Ensure the output is mp4 if separate streams are downloaded
    }

    # Use yt-dlp to download the video
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([src])

    # Check if the file is downloaded and exists
    if os.path.exists(output_file):
        # Open the downloaded video file using OpenCV
        cap = cv2.VideoCapture(output_file)

        # Read and process the video
        decode_video(cap, dest_folder)
    else:
        print("Failed to download video.")
        sys.exit(1)

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print("Usage: python video2file.py \"https://video_url\" destination_folder")
        sys.exit(1)

    src = sys.argv[1]
    dest_folder = sys.argv[2]
    youtube_decode(src, dest_folder)