python: batch compression replacement program for computer hard disk video files

Keywords: Python

As a technician, many videos stored on the computer hard disk are tutorials and videos, not movies.
These long-term video files will be of great help to us if they can be compressed effectively.

I usually use the pill toolbox for compression. This is currently recognized as the best compression software.
Baidu online disk link: Extraction code: 6666

The functions of this software are:
1. Use software function 1 to scan all video files in a partition of the computer and copy them into one folder.
2. The user manually uses the pill toolbox for compression.
3. Use software function 2 to replace the video file.

Description: small pill toolbox can be used for batch pressing of multiple videos. Suppress coding, I usually choose x264_32_8bit, the default CRF parameter is 23 or 23.5.

Background: he returned from Mali in Africa last week and is now isolated in Shanghai. After reading the python tutorial, I wrote this very practical program for me, the first Python program.

Source code

There are many sub functions in the source code, and some are relatively simple. Not one by one.
Resources in:

Project ideas

  1. Using the function 1 of this software, the user selects to copy the qualified videos of disk E to the specified folder.
  2. The user manually starts the "pill toolbox" to convert the video.
  3. The user continues to use the function 2 of the software and selects to replace the converted video with the original video.

I used this software to convert 480 videos in E disk, with a total video size of about 80G.

Function 1: copy

When the user selects function 1, the path will be entered to realize replication.

if __name__ == '__main__':
    MessageFilePath = 'CopyMessage.xlsx'
    postfix = '_batch.mp4'  # Suffix NOUN
    #ChoiceNum =2    #When testing, it is convenient.
    if ChoiceNum==1:
        source_dir, target_dir = ChoiceDirFunction()
        #source_dir='E: / internal training materials of the company / explanation of ultrasonic sensor on January 18, 2016 '
        #target_dir='E: / do not use hidden dragons'

        Filelist = getAllSub(source_dir)
        FileListResult = []
        CopyMessage = Configuration(FileListResult,target_dir)
        xw_toExcel(CopyMessage, MessageFilePath)
        for MessageRow in CopyMessage:
            if not Num:
                print('Copy of this file failed',MessageRow[0],MessageRow[1])
            print('Current replication progress is{0}/{1}'.format(str(i),str(len(CopyMessage))))

Function 2: replace

When the user selects function 2, video file replacement will be realized.

    elif ChoiceNum==2:
        print('\n Before replacing the compressed video with the original path video, you can CopyMessage.xlsx Configure the third column in the file and choose not to replace some files.\n')
        ti=str(input('Do you want to start replacing files? y/n ='))
        if ti=='y':
            ReadMessage = []
            ReadMessage = ReadCopyMessage(MessageFilePath)
            for MessageRow in ExM:
                if MessageRow[2]>0:    #Enable switch
                    if not Num:
                        print('Copy of this file failed',MessageRow[4],MessageRow[5])
                        if os.path.exists(MessageRow[5])&os.path.exists(MessageRow[0]):  #After confirming that the copy is successful, delete the original video file.
                            #print('deleted: ', MessageRow[0])

                print('****Current replication progress is{0}/{1}\t\t The destination path is:{2}'.format(str(i), str(len(ExM)),MessageRow[5]))
                i += 1

                '''if i >= 1:  # For testing. Run only a few files for testing.

            xw_toExcel(ExM, MessageFilePath)
            print('\n\t The replacement work has been completed.\n\t You can',MessageFilePath,'Table file viewing conversion results')
            print('\t You can delete it manually',PathTar,'All files in the folder.')

Posted by Leshiy on Fri, 19 Nov 2021 19:15:02 -0800