Video media batch renaming tool made with Matlab

Video media batch renaming tool

1.Abstract

  Programming language: Matlab, python
  Idea: use python to obtain video creation time, realize reading and display of video key frames through Matlab, and realize renaming by combining user marking information with video creation time.
  Tools used: VideoReader (MatLab), pymediainfo (python)

2. Intro

  In actual work, we encounter the situation that video materials are exported from the machine in batches. Because there are too many materials, it is unrealistic to open and check the content one by one. Some computers generate thumbnails that are not clear, and you need to manually rename all files yourself. In order to solve the above problems and realize batch marking for a large number of video files, the following work has been done. The article explains according to different parts and the problems encountered in the process of realizing this part. Resources that need to be supplemented will be attached.

3. Demand Analysis

  In ordinary work and life, there will be a large amount of video materials that need to be collected and organized. The information I need is similar to photo management, which contains information such as the time and location of the file shooting. Since not all file systems can generate viewable thumbnails, this makes it impossible to quickly understand the content of the video. So, I hope to be able to see the above information directly in the file name. In order to obtain the shooting time of the video file , I choose to use pymediainfo in python to read the file information and select the information I need. For the location information, since the shooting device does not have the recording function, I choose to use Matlab 's VideoReader to read and display a certain frame in the video; the user can mark the location and brief content. This can also easily read the keyframes containing the record information, which is convenient for post-processing personnel. After determining the requirements and methods, it is the implementation process.

4. Python part

def infoget(FilePath):
    from pymediainfo import MediaInfo
    media_info = MediaInfo.parse(FilePath)
    data = media_info.to_data()
    info=data["tracks"][0]["file_last_modification_date__local"]
    return info

  Here, the acquisition of video information is achieved by encapsulating functions. The data data obtained in L4 contains a lot of video information, which can be obtained according to your own needs. If you need to DIY by yourself, you can analyze all the information in it first, and then select the information you need. And what I directly choose here is " file_last_modification_date__local " as the creation time of the video file. Note that the time selected here is your internal camera time, that is to say, you need to set your camera time correctly from the beginning. The time text obtained here is (example): 2022-04-14 17:40:24.000
  In order to facilitate the viewing of character processing effects, I choose to directly return this string to Matlab for normalization. The installation of pymedia can use pip directly , so I won’t go into details here.

5. Matlab part

  In Matlab I chose to use three .m files to implement the functionality. getpic is used to obtain time and key frames; dealvideo_2 is responsible for displaying key frames, collecting user marking information, and completing calls to the first two functions and renaming files in vidmainop .

5.1 Acquisition of time and key frames

function  [time,piclocation,vidlocation,totalnum]=getpic(FilePath)
%%初始化
finloca = [FilePath,'\*.mp4'];
%检索并列表
File= dir (finloca);
viddata = {File.name};totalnum = length(viddata);
%三个cell的初始化
piclocation =cell(1,totalnum);vidlocation=cell(1,totalnum);
time=cell(1,totalnum);
%对每个文件进行处理
for i = 1:totalnum
    vidlocation{i} = [FilePath,'\',viddata{i}];
    piclocation{i} = [FilePath,'\',viddata{i}(1:end-4),'_1.jpg'];
    vidobj = VideoReader(vidlocation{i});
    pic1 = read(vidobj,[1]);imwrite(pic1,piclocation{i});
    %获取、处理时间信息
    str = py.info.infoget(vidlocation{i});
    str = char(str(1:end-4));
    k = strfind(str,' ');str(k) = '_';
    k = strfind(str,'-');str(k) = '';
    k = strfind(str,':');str(k) = '_';
    time{i} = str;
end
disp('缩略图处理完成')
end

  Here, all MP4 files under FilePath are retrieved, and the file name and quantity are obtained.
  Use VideoReader to get the video stream, and then use read to read the specified frame. If you need to read frames at a specified time point, you need to convert time and frame rate. Then use imwrite to save the frame in the folder in the format of "filename+_1.jpg".
  Then ***py.info.infoget(vidlocation{i})*** implements the call to info.py. The format here is "py.filename.functionname(parameters)". Then you will get the previous time information string. The string here will be recognized as pystr by Matlab, so we need to convert the format to prevent post-processing errors. For the time information: 2022-04-14 17:40:24.000 , what we need to pay attention to is that the spaces and "-" here are illegal in the file name, so I will replace these characters later. The interception of the event is to ensure that the length of the file name is reasonable, and ignoring the information after the second will not cause the two files to have the same name. The processed form is: " 20220414_17_40_24 ". Here, if the video file is high resolution and the keyframes you select are located at a later time sequence, this will significantly increase the processing time of this link. So I suggest to take only the first frame. If there is a special need, I also set a prompt message. 1
  The parameters returned by the function are: time, piclocation, vidlocation: time, all pictures and video paths; totalnum the total number of files.

5.2 Information Collection

function [vidloca,videvent]=dealvideo_2(piclocation,totalnum)
%完成对图片以及视频的检索 
%初始化
vidloca =cell(1,totalnum);videvent=cell(1,totalnum);
%打标工作
for i = 1:totalnum
    pic = imshow(piclocation{i});
    showtext = strcat("请输入地点(精确)/事件",num2str(i),":\n");
    k=[];
    while isempty(k)
        sort = input(showtext,'s');
        k = strfind(sort,'/');
    end
    if isempty(k)
        disp('NONE')
    else
        info = strsplit(sort,'/');
        vidloca{i} = info{1};videvent{i}= info{2};
    end
    close all;
end

  Simple information processing and storage. Mainly by displaying keyframes and then asking for user input. Data validation is added here.

5. Triple Naming

%%初始化
clc;clear;
FilePath = 'D:\xx';
[time,piclocation,vidlocation,totalnum] = getpic(FilePath);
[vidloca,videvent]=dealvideo_2(piclocation,totalnum);
%finalname = cell(1:totalnum);
for i = 1:totalnum
    finalname = strcat(time{i},'_',vidloca{i},'_',videvent{i},'.MP4');
    newname = strcat(FilePath,'\',finalname);
    movefile(vidlocation{i},newname); 
end

  Here you need to enter your video storage folder" FilePath = 'D:\xx'; "

6. Environment configuration

  There may be problems in this process: 1. Matlab's call to python; 2. The configuration of Videoreader in Matlab

6.1 Matlab calls python

1. Manually add the python path

pyversion \python解释器路径

  It should be noted here that the number of digits of Matlab and python must be the same. Then use pyversion to check whether the initialization is correct. If it is correct, the following output will be output

version: '3.9'
executable: 'C:\Users\Administrator\AppData\Local\Programs\Python\Python39\pythonw.exe'
library: 'C:\Users\Administrator\AppData\Local\Programs\Python\Python39\python39.dll'
home: 'C:\Users\Administrator\AppData\Local\Programs\Python\Python39'
isloaded: 1

  If it is correct, the python function can be used. It is recommended to put the py file and the m file in the same folder. If the python file is modified during debugging, the file needs to be reloaded, but it is not required during normal operation.

6.2 Configuration of Videoreader in Matlab

  When using the videoreader function to read MP4 video files, there will be a prompt that the internal resources cannot be initialized; you need to install ffmpeg and the codecs of the corresponding operating system.

  The specific steps here have the solutions already given, see this article for details:

https://blog.csdn.net/fzq_yu/article/details/110731098

  For the installation of codecs in the fourth step in the original text, I will give the installation package corresponding to win10 here, and you can use it yourself.

链接:https://pan.baidu.com/s/1tinOnR8EjK9xUTERr1ywWQ 
提取码:1949 
--来自百度网盘超级会员V4的分享

7.Conclusion

  Through this Matlab, a large number of video files can be semi-automatically processed, and the file information can be intuitively displayed in the file name.


  1. Here, file conversion can be used to reduce clarity to meet the needs of special keyframe reading; ↩︎

Guess you like

Origin blog.csdn.net/qq_49003248/article/details/124167561