File size: 4,172 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
102
103
104
105
106
107
108
109
110
111
112
113
114
python file2video.py --encode go.txt out.mp4 
python file2video.py --decode out.mp4 ./     
```python

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)



```
```python

from encode import create_video

from decode_video import decode

from youtube_decode import youtube_decode

import argparse

import sys



def enc_file(source_file, output_video):

    print (f"Encoding {source_file} to {output_video}")

    create_video(source_file, output_video)



def dec_video(source_video, destination_folder, docker_mode):

    print (f"Decoding {source_video} to {destination_folder}")

    decode(source_video, destination_folder)



def y_decode(video_url, destination_folder, docker_mode):

    print (f"Decoding {video_url} to {destination_folder}")

    youtube_decode(video_url, destination_folder)



def main():



    # First, check if '--docker' is in the command line arguments

    docker_mode = '--docker' in sys.argv

    if docker_mode:

        sys.argv.remove('--docker')  # Remove it so it doesn't interfere with the main parser



    docker_usage = """\

docker run --rm -v $(pwd):/data karaketir16/file2video [-h]  [--encode source_file output_video] 

                                                        [--decode source_video destination_folder] 

                                                        [--youtube-decode youtube_url destination_folder]"""

    

    if docker_mode:

        parser = argparse.ArgumentParser(description="Program to encode files into videos and decode videos back to files.", usage=docker_usage)

    else:

        parser = argparse.ArgumentParser(description="Program to encode files into videos and decode videos back to files.")



    # Optional argument for encoding

    parser.add_argument("--encode", nargs=2, metavar=('source_file', 'output_video'), 

                        help="Encode a file into a video: source_file output_video.mp4")

    

    # Optional argument for decoding

    parser.add_argument("--decode", nargs=2, metavar=('source_video', 'destination_folder'), 

                        help="Decode a video to a file: source_video.mp4 destination_folder")

    

    # Optional argument for YouTube video decoding

    parser.add_argument("--youtube-decode", nargs=2, metavar=('youtube_url', 'destination_folder'), 

                        help="Decode a video from a YouTube URL to a file: 'youtube_url' destination_folder")



    

    args = parser.parse_args()



    # Check which command is used and call the corresponding function

    if args.encode:

        enc_file(*args.encode)

    elif args.decode:

        dec_video(*args.decode, docker_mode)

    elif args.youtube_decode:

        y_decode(*args.youtube_decode, docker_mode)

    else:

        parser.print_help()



if __name__ == "__main__":

    main()

```