File size: 12,138 Bytes
52409f1
 
 
 
 
 
d348068
52409f1
 
 
 
 
 
b19f57b
52409f1
f405812
 
 
d348068
 
 
 
52409f1
b1cd1c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d68759e
52409f1
 
 
 
 
 
 
 
 
 
 
 
 
d68759e
52409f1
d68759e
 
 
52409f1
 
 
 
d68759e
 
52409f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b19f57b
d68759e
 
c237b6b
d68759e
 
c237b6b
 
 
 
d68759e
 
52409f1
 
 
 
 
b4ce27c
 
 
6776e8c
 
52409f1
 
 
 
 
2c75576
 
 
 
6776e8c
52409f1
 
d68759e
 
 
 
6776e8c
2c75576
 
 
52409f1
 
d348068
 
 
 
 
 
b1cd1c8
52409f1
 
 
 
 
 
 
 
 
 
 
b19f57b
52409f1
 
 
 
8e6b896
 
 
d68759e
8e6b896
d68759e
8e6b896
 
b1cd1c8
52409f1
b4ce27c
 
 
 
 
 
 
 
52409f1
b4ce27c
 
 
 
 
 
 
 
 
52409f1
b4ce27c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52409f1
b4ce27c
 
 
 
 
 
 
 
 
 
52409f1
 
b4ce27c
52409f1
 
b4ce27c
52409f1
b4ce27c
52409f1
 
 
 
 
 
b1cd1c8
 
 
52409f1
 
 
 
 
4cda010
b19f57b
b1cd1c8
 
 
 
 
 
 
 
b19f57b
b1cd1c8
 
 
 
 
 
 
52409f1
 
 
 
b19f57b
52409f1
 
b1cd1c8
 
 
52409f1
 
 
 
b1cd1c8
 
 
 
 
 
 
 
 
 
 
 
c237b6b
b1cd1c8
 
 
e75d696
 
b19f57b
b1cd1c8
 
e75d696
52409f1
 
b4ce27c
b1cd1c8
 
52409f1
 
b1cd1c8
52409f1
b1cd1c8
 
52409f1
 
b19f57b
 
52409f1
cee1bed
c237b6b
52409f1
 
 
 
 
 
 
291cd48
52409f1
 
b1cd1c8
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import gradio as gr

from PIL import Image
from moviepy.editor import VideoFileClip, AudioFileClip

import os
from openai import OpenAI
import subprocess
from pathlib import Path
import uuid
import tempfile
import shlex
import shutil
from utils import format_bash_command

HF_API_KEY = os.environ["HF_TOKEN"]
print('caca')
print(os.environ["HF_TOKEN"])
client = OpenAI(
    base_url="https://api-inference.huggingface.co/v1/",
    api_key=HF_API_KEY
)

allowed_medias = [
    ".png",
    ".jpg",
    ".jpeg",
    ".tiff",
    ".bmp",
    ".gif",
    ".svg",
    ".mp3",
    ".wav",
    ".ogg",
    ".mp4",
    ".avi",
    ".mov",
    ".mkv",
    ".flv",
    ".wmv",
    ".webm",
    ".mpg",
    ".mpeg",
    ".m4v",
    ".3gp",
    ".3g2",
    ".3gpp",
]


def get_files_infos(files):
    results = []
    for file in files:
        file_path = Path(file.name)
        info = {}
        info["size"] = os.path.getsize(file_path)
        info["name"] = file_path.name
        file_extension = file_path.suffix

        if file_extension in (".mp4", ".avi", ".mkv", ".mov"):
            info["type"] = "video"
            video = VideoFileClip(file.name)
            info["duration"] = video.duration
            info["dimensions"] = "{}x{}".format(video.size[0], video.size[1])
            if video.audio:
                info["type"] = "video/audio"
                info["audio_channels"] = video.audio.nchannels
            video.close()
        elif file_extension in (".mp3", ".wav"):
            info["type"] = "audio"
            audio = AudioFileClip(file.name)
            info["duration"] = audio.duration
            info["audio_channels"] = audio.nchannels
            audio.close()
        elif file_extension in (
            ".png",
            ".jpg",
            ".jpeg",
            ".tiff",
            ".bmp",
            ".gif",
            ".svg",
        ):
            info["type"] = "image"
            img = Image.open(file.name)
            info["dimensions"] = "{}x{}".format(img.size[0], img.size[1])
        results.append(info)
    return results


def get_completion(prompt, files_info, top_p, temperature):
    files_info_string = ""
    for file_info in files_info:
        files_info_string += f"""{file_info["type"]} {file_info["name"]}"""
        if file_info["type"] == "video" or file_info["type"] == "image":
            files_info_string += f""" {file_info["dimensions"]}"""
        if file_info["type"] == "video" or file_info["type"] == "audio":
            files_info_string += f""" {file_info["duration"]}s"""
        if file_info["type"] == "audio" or file_info["type"] == "video/audio":
            files_info_string += f""" {file_info["audio_channels"]} audio channels"""
        files_info_string += "\n"

    messages = [
        {
            "role": "system",
            # "content": f"""Act as a FFMPEG expert. Create a valid FFMPEG command that will be directly pasted in the terminal. Using those files: {files_info} create the FFMPEG command to achieve this: "{prompt}". Make sure it's a valid command that will not do any error. Always name the output of the FFMPEG command "output.mp4". Always use the FFMPEG overwrite option (-y). Don't produce video longer than 1 minute. Think step by step but never give any explanation, only the shell command.""",
            # "content": f"""You'll need to create a valid FFMPEG command that will be directly pasted in the terminal. You have those files (images, videos, and audio) at your disposal: {files_info} and you need to compose a new video using FFMPEG and following those instructions: "{prompt}". You'll need to use as many assets as you can. Make sure it's a valid command that will not do any error. Always name the output of the FFMPEG command "output.mp4". Always use the FFMPEG overwrite option (-y). Try to avoid using -filter_complex option.  Don't produce video longer than 1 minute. Think step by step but never give any explanation, only the shell command.""",
            "content": """
You are a very experienced media engineer, controlling a UNIX terminal.
You are an FFMPEG expert with years of experience and multiple contributions to the FFMPEG project.

You are given:
(1) a set of video, audio and/or image assets. Including their name, duration, dimensions and file size
(2) the description of a new video you need to create from the list of assets

Based on the available assets and the description, your objective issue a FFMPEG to create a new video using the assets.
This will often involve putting assets one after the other, cropping the video format, or playing music in the background. Avoid using complex FFMPEG options, and try to keep the command as simple as possible as it will be directly paster into the terminal.
""",
        },
        {
            "role": "user",
            "content": f"""Always output the media as video/mp4 and output file with "output.mp4". Provide only the shell command without any explanations.
The current assets and objective follow. Reply with the FFMPEG command:

AVAILABLE ASSETS LIST:

{files_info_string}

OBJECTIVE: {prompt} and output at "output.mp4"
YOUR FFMPEG COMMAND:
         """,
        },
    ]
    try:
        completion = client.chat.completions.create(
            model="Qwen/Qwen2.5-Coder-32B-Instruct",
            messages=messages,
            temperature=temperature,
            top_p=top_p,
            max_tokens=2048
        )
        command = completion.choices[0].message.content.replace("\n", "")

        # remove output.mp4 with the actual output file path
        command = command.replace("output.mp4", "")

        return command
    except Exception as e:
        print("FROM OPENAI", e)
        raise Exception("OpenAI API error")


def update(files, prompt, top_p=1, temperature=1):
    if prompt == "":
        raise gr.Error("Please enter a prompt.")

    files_info = get_files_infos(files)
    # disable this if you're running the app locally or on your own server
    for file_info in files_info:
        if file_info["type"] == "video":
            if file_info["duration"] > 120:
                raise gr.Error(
                    "Please make sure all videos are less than 2 minute long."
                )
        if file_info["size"] > 10000000:
            raise gr.Error("Please make sure all files are less than 10MB in size.")

    attempts = 0
    while attempts < 2:
        print("ATTEMPT", attempts)
        try:
            command_string = get_completion(prompt, files_info, top_p, temperature)
            print(
                f"""///PROMTP {prompt} \n\n/// START OF COMMAND ///:\n\n{command_string}\n\n/// END OF COMMAND ///\n\n"""
            )

            # split command string into list of arguments
            args = shlex.split(command_string)
            if args[0] != "ffmpeg":
                raise Exception("Command does not start with ffmpeg")
            temp_dir = tempfile.mkdtemp()
            # copy files to temp dir
            for file in files:
                file_path = Path(file.name)
                shutil.copy(file_path, temp_dir)

            # test if ffmpeg command is valid dry run
            ffmpg_dry_run = subprocess.run(
                args + ["-f", "null", "-"],
                stderr=subprocess.PIPE,
                text=True,
                cwd=temp_dir,
            )
            if ffmpg_dry_run.returncode == 0:
                print("Command is valid.")
            else:
                print("Command is not valid. Error output:")
                print(ffmpg_dry_run.stderr)
                raise Exception(
                    "FFMPEG generated command is not valid. Please try again."
                )

            output_file_name = f"output_{uuid.uuid4()}.mp4"
            output_file_path = str((Path(temp_dir) / output_file_name).resolve())
            subprocess.run(args + ["-y", output_file_path], cwd=temp_dir)
            generated_command = f"### Generated Command\n```bash\n{format_bash_command(args)}\n    -y output.mp4\n```"
            return output_file_path, gr.update(value=generated_command)
        except Exception as e:
            attempts += 1
            if attempts >= 2:
                print("FROM UPDATE", e)
                raise gr.Error(e)


with gr.Blocks() as demo:
    gr.Markdown(
        """
            # 🏞 GPT-4 Video Composer
            Add video, image and audio assets and ask ChatGPT to compose a new video.
            **Please note: This demo is not a generative AI model, it only uses GPT-4 to generate a valid FFMPEG command based on the input files and the prompt.**
        """,
        elem_id="header",
    )
    with gr.Row():
        with gr.Column():
            user_files = gr.File(
                file_count="multiple",
                label="Media files",
                file_types=allowed_medias,
            )
            user_prompt = gr.Textbox(
                placeholder="I want to convert to a gif under 15mb",
                label="Instructions",
            )
            btn = gr.Button("Run")
            with gr.Accordion("Parameters", open=False):
                top_p = gr.Slider(
                    minimum=-0,
                    maximum=1.0,
                    value=1.0,
                    step=0.05,
                    interactive=True,
                    label="Top-p (nucleus sampling)",
                )
                temperature = gr.Slider(
                    minimum=-0,
                    maximum=5.0,
                    value=1.0,
                    step=0.1,
                    interactive=True,
                    label="Temperature",
                )
        with gr.Column():
            generated_video = gr.Video(
                interactive=False, label="Generated Video", include_audio=True
            )
            generated_command = gr.Markdown()

        btn.click(
            fn=update,
            inputs=[user_files, user_prompt, top_p, temperature],
            outputs=[generated_video, generated_command],
        )
    with gr.Row():
        gr.Examples(
            examples=[
                [
                    [
                        "./examples/cat8.jpeg",
                        "./examples/cat1.jpeg",
                        "./examples/cat2.jpeg",
                        "./examples/cat3.jpeg",
                        "./examples/cat4.jpeg",
                        "./examples/cat5.jpeg",
                        "./examples/cat6.jpeg",
                        "./examples/cat7.jpeg",
                        "./examples/heat-wave.mp3",
                    ],
                    "make a video gif, each image with 1s loop and add the audio as background",
                    0,
                    0,
                ],
                [
                    ["./examples/example.mp4"],
                    "please encode this video 10 times faster",
                    0,
                    0,
                ],
                [
                    ["./examples/heat-wave.mp3", "./examples/square-image.png"],
                    "Make a 720x720 video, a white waveform of the audio, and finally add add the input image as the background all along the video.",
                    0,
                    0,
                ],
                [
                    ["./examples/waterfall-overlay.png", "./examples/waterfall.mp4"],
                    "Add the overlay to the video.",
                    0,
                    0,
                ],
            ],
            inputs=[user_files, user_prompt, top_p, temperature],
            outputs=[generated_video, generated_command],
            fn=update,
            run_on_click=True,
            cache_examples=True,
        )

    with gr.Row():
        gr.Markdown(
            """
            If you have idea to improve this please open a PR:

            [![Open a Pull Request](https://huggingface.co/datasets/huggingface/badges/raw/main/open-a-pr-lg-light.svg)](https://huggingface.co/spaces/huggingface-projects/video-composer-gpt4/discussions)
            """,
        )
demo.queue(api_open=False)
demo.launch(show_api=False)