Article | doug \

Source: Python Technology “ID: pythonall”

The essential difference between humans and animals is that humans know how to make and use tools, which is determined by genes from ancient times.

Some time ago, a business of my friend needed a large number of original short videos, so he asked me if I could help him. After understanding the specific requirements, I found that it did not need high quality content, and the operation was not very complicated, and it could be completely automated through Python, so I agreed to do so.

We know that PyAutoGUI is a Python library that programmatically controls the mouse and keyboard, so we started with it.

The installation

Windows and macOS have different installation commands.

## windows
py -m pip install pyautogui

## macOS
python3 -m pip install pyautogui
Copy the code

The screen position

The positions on the screen are represented by cartesian coordinates, where X starts at 0 in the upper left corner and increases to the right, unlike math, Y starts at 0 in the upper left corner and increases downward.

So the pixel in the upper left corner is at coordinate (0, 0), and if your screen resolution is 1920 x 1080, the pixel in the lower right corner will be (1919, 1079) because the pixel starts at coordinate 0. The screen resolution can be obtained from the size() function, and the current mouse position can be obtained from the position() function.

0.0       X increases -->
+---------------------------+
|                           | Y increases
|                           |     |
|   1920 x 1080 screen      |     |
|                           |     V
|                           |
|                           |
+---------------------------+ 1919.1079
Copy the code
import pyautogui

x, y = pyautogui.position()
print(x, y)

x, y = pyautogui.size()
print(x, y) ##545 437
1440 900
Copy the code

The mouse moves

The movement of the mouse is divided into absolute coordinate movement and relative coordinate movement.

For example, if you want to move the mouse to the coordinates (100,100), call the moveTo function.

importPyautogui # Move the mouse over (100.200) coordinates pyAutogui.moveto (100.200# Move the mouse pointer to (100.500Coordinate in pyautogui. MoveTo (None,500# Move the mouse pointer to (600.500) coordinates pyAutogui.moveto (600, None) # move the mouse over (100.200), the moving process is a continuous process and time-consuming2Second pyautogui. MoveTo (100.200.2)
Copy the code

The move function is a simple relative move.

importPyautogui # Move the mouse over (100.200) coordinates pyAutogui.moveto (100.200# Move the mouse down50Pixel pyautogui. Move (0.50) # Move the mouse to the left30Pixel pyautogui. Move (- 30.0) # Move the mouse to the left30Pixel pyautogui. Move (- 30, None)
Copy the code

The mouse to drag and drop

Compared with mouse movement, mouse drag is also divided into absolute coordinate drag and relative coordinate drag. Similarly, dragTo uses absolute coordinates and drag uses relative coordinates.

importPyautogui # Hold down the left mouse button and drag the target to (100.200) coordinates pyAutogui.dragto (100.200, button='left'# Hold the left mouse button and drag the target to (100.200), the moving process is a continuous process and time-consuming2Second pyautogui. DragTo (100.200.2, button='left') # Hold down the right mouse button to move the target to the right30Pixel, the moving process is a continuous process, time-consuming2Second pyautogui. Drag (30.0.2, button='right')
Copy the code

The mouse to click

We can simulate mouse clicks with the click() function.

importPyautogui.click () # Move the mouse to (100.200MoveTo + click pyautogui.click(x=100, y=200) # Left-click pyAutogui.Doubleclick () in the current positionCopy the code

Control of the keyboard

Keyboard controls typically involve typing strings, pressing a key, and key combinations.

importPyautogui # Enter "Hello World" pyAutogui.write ('Hello world! '# Press Enter/F1 pyAutogui.press ('enter')
pyautogui.press('f1'Pyautogui.hotkey (pyAutogui.hotkey)'command'.'a')
Copy the code

Making video

With the above foundation, we can start our video production, this time using the cutting software.

Here you need to be familiar with the operation of clip, which is basically divided into three parts: drag the specified image into the video track, adjust the video length, export.

The final effect is as follows:

# coding=utf- 8 -
import time
importDef drag_img_to_track(): # select pyautogui.moveto (170.270Pyautogui.doubleclick () # Drag image to track pyAutogui.dragto ()120.600.1, button='left'Def drag_IMG_to_3_min (): # select pyautogui.moveto (125.600) pyAutogui.click () # drag to 3min pyAutogui.moveto (135.600)
    pyautogui.dragTo(700.600.1, button='left'Def delete_top_img(): # Delete the second image in the track pyautogui.moveto (300.160)
    pyautogui.doubleClick()
    pyautogui.press("backspace")

    # enter yes
    pyautogui.moveTo(650.470)
    time.sleep(0.5Def export(name): pyAutogui.moveto ()126.600)
    pyautogui.click()

    pyautogui.hotkey('command'.'e')
    pyautogui.write(name)
    time.sleep(1)
    pyautogui.moveTo(800.393)
    pyautogui.click()
    time.sleep(20)
    pyautogui.click()

index = 0
count = 2
while index < count:
    drag_img_to_track()
    drag_img_to_3_min()
    delete_top_img()
    export(str(index))
    time.sleep(2)
    index += 1
    print("end..." + str(index))
Copy the code

conclusion

Today we have fully automatic video production via PyAutoGUI, we just need to import the material into the clip.

Since only a small portion of the GIF is recorded, you can control how many videos are made by adjusting the count value in the main function.

PS: Reply “Python” within the public number to enter the Python novice learning exchange group, together with the 100-day plan!

Old rules, brothers still remember, the lower right corner of the “watching” click, if you feel the content of the article is good, remember to share moments to let more people know!

[Code access ****]

Identify the qr code at the end of the article, reply: 210701