day84/code-server
0
1python file2video.py --encode go.txt out.mp4
2python file2video.py --decode out.mp4 ./
3```python
4import cv2
5import sys
6import yt_dlp
7import os
8from datetime import datetime
9
10from decode_video import decode_video
11
12def youtube_decode(src, dest_folder):
13 base_filename = "downloaded_video"
14 timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
15 output_file = f"{dest_folder}/{base_filename}_{timestamp}.mp4"
16
17 # Create a yt-dlp configuration to download the video
18 ydl_opts = {
19 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
20 'outtmpl': output_file,
21 'noplaylist': True,
22 'quiet': True,
23 'merge_output_format': 'mp4', # Ensure the output is mp4 if separate streams are downloaded
24 }
25
26 # Use yt-dlp to download the video
27 with yt_dlp.YoutubeDL(ydl_opts) as ydl:
28 ydl.download([src])
29
30 # Check if the file is downloaded and exists
31 if os.path.exists(output_file):
32 # Open the downloaded video file using OpenCV
33 cap = cv2.VideoCapture(output_file)
34
35 # Read and process the video
36 decode_video(cap, dest_folder)
37 else:
38 print("Failed to download video.")
39 sys.exit(1)
40
41if __name__ == '__main__':
42 if len(sys.argv) < 3:
43 print("Usage: python video2file.py \"https://video_url\" destination_folder")
44 sys.exit(1)
45
46 src = sys.argv[1]
47 dest_folder = sys.argv[2]
48 youtube_decode(src, dest_folder)
49
50```
51```python
52from encode import create_video
53from decode_video import decode
54from youtube_decode import youtube_decode
55import argparse
56import sys
57
58def enc_file(source_file, output_video):
59 print (f"Encoding {source_file} to {output_video}")
60 create_video(source_file, output_video)
61
62def dec_video(source_video, destination_folder, docker_mode):
63 print (f"Decoding {source_video} to {destination_folder}")
64 decode(source_video, destination_folder)
65
66def y_decode(video_url, destination_folder, docker_mode):
67 print (f"Decoding {video_url} to {destination_folder}")
68 youtube_decode(video_url, destination_folder)
69
70def main():
71
72 # First, check if '--docker' is in the command line arguments
73 docker_mode = '--docker' in sys.argv
74 if docker_mode:
75 sys.argv.remove('--docker') # Remove it so it doesn't interfere with the main parser
76
77 docker_usage = """\
78docker run --rm -v $(pwd):/data karaketir16/file2video [-h] [--encode source_file output_video]
79 [--decode source_video destination_folder]
80 [--youtube-decode youtube_url destination_folder]"""
81
82 if docker_mode:
83 parser = argparse.ArgumentParser(description="Program to encode files into videos and decode videos back to files.", usage=docker_usage)
84 else:
85 parser = argparse.ArgumentParser(description="Program to encode files into videos and decode videos back to files.")
86
87 # Optional argument for encoding
88 parser.add_argument("--encode", nargs=2, metavar=('source_file', 'output_video'),
89 help="Encode a file into a video: source_file output_video.mp4")
90
91 # Optional argument for decoding
92 parser.add_argument("--decode", nargs=2, metavar=('source_video', 'destination_folder'),
93 help="Decode a video to a file: source_video.mp4 destination_folder")
94
95 # Optional argument for YouTube video decoding
96 parser.add_argument("--youtube-decode", nargs=2, metavar=('youtube_url', 'destination_folder'),
97 help="Decode a video from a YouTube URL to a file: 'youtube_url' destination_folder")
98
99
100 args = parser.parse_args()
101
102 # Check which command is used and call the corresponding function
103 if args.encode:
104 enc_file(*args.encode)
105 elif args.decode:
106 dec_video(*args.decode, docker_mode)
107 elif args.youtube_decode:
108 y_decode(*args.youtube_decode, docker_mode)
109 else:
110 parser.print_help()
111
112if __name__ == "__main__":
113 main()
114```