[PYTHON] Import serial number videos together with Aviutl

Introduction

The purpose of this article is to automatically import the videos with serial numbers created as a result of jampcutting the videos using the article (https://qiita.com/merarli/items/1b7819adbae7fad3c9b4) into Aviutl. I will. __ Therefore, it is assumed that the following videos with serial numbers exist. フォルダ.PNG

Motivation for creation

I succeeded in automatically jump-cutting with the great leader __ (https://qiita.com/merarli/items/1b7819adbae7fad3c9b4/)__, but the next thing I'm waiting for is Aviutl. It was to drag to the extended timeline of. I automated jump cuts to make money by posting videos with great effort, but this is meaningless ... Well, what should I do! !! I created it with the motive of.

result

I will put the code created earlier.


import glob#Get list
import pathlib#It seems that this one similar to glob is more convenient
import subprocess
from re import sub
from os import path#Path connection
import sys

m_path=r"D:\movie\JumpCut_note_DT"#Video path
file_exo=open(path.join(m_path,"make_exo.exo"),mode='w')
#Video data import pathlib
l_movie = pathlib.Path(m_path).glob(r'**\*.mp4')
movie_name=[]#For storing file names
for p in l_movie:
    movie_name.append(p.name)#Add elements
    print(p.name)
num= lambda val : int(sub('\\D','', val))#Extract numbers from file names
movie_name=sorted(movie_name,key = num)#sort
print(movie_name)


#Initial setting output Read with ffmpeg
movie_size=subprocess.run(["ffprobe","-v","error","-select_streams","v:0","-show_entries","stream=width,height,r_frame_rate","-of","default=nokey=1:noprint_wrappers=1",path.join(m_path,movie_name[1])],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
movie_size = movie_size.stdout.decode("utf8")
movie_size = movie_size.splitlines()#List storage separated by newline characters

print(movie_name[0])
print("movie_size\n"+ movie_size[0])
print(path.join(m_path,movie_name[0]))

print("[exedit]",file=file_exo)
print("width=",movie_size[0],sep="",file=file_exo)
print("hight=",movie_size[1],sep="",file=file_exo)
print("rate=",eval(movie_size[2]),sep="",file =file_exo)#Character string calculation function eval
print("scale=",1,sep="",file =file_exo)
print("langh=",1,sep="",file=file_exo)
print("audio_rate=",44100,sep="",file=file_exo)
print("audio_ch=",2,sep="",file=file_exo)

#exo creation
flame_all = []#Number of video frames
n=0#Number of repetitions
s_flame=1#Beginning frame
e_flame=0#End frame
#Video data
for m_num in movie_name:
    n=movie_name.index(m_num)#Number of repetitions
    #[]Video order
    print('[',n,']',sep="",file=file_exo)#sep delimiter default half-width space
    #Video start-end
    flame =subprocess.run(["ffprobe","-v","error","-select_streams","v:0","-show_entries","stream=nb_frames","-of","default=nokey=1:noprint_wrappers=1",path.join(m_path,m_num)],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    flame_all.append(flame.stdout.decode("utf8"))#The return value of subprocess is byte?So convert to utf8
    flame_all[n]=flame_all[n].rstrip('\r\n')#Delete the newline character at the end
    e_flame = int(flame_all[n])+int(e_flame)#Current number of flames+Number of frames at the end of the last time
    print('start=',s_flame,sep="",file=file_exo)
    s_flame=int(e_flame)+1#Number of next start frames=Number of frames at the end of the last time+1
    print('end=',e_flame,sep="",file=file_exo)
    print('layer=',1,sep="",file=file_exo)#Video file 1,Audio file 2
    print('group=',n+1,sep="",file=file_exo)
    print('overlay=',1,sep="",file=file_exo)
    print('camera=',0,sep="",file=file_exo)
    print('[''{:.1f}'']'.format(n),sep="",file=file_exo)
    print('_name=Video file',sep="",file=file_exo)#Make an audio file for an audio file
    print('Playback position=',1,sep="",file=file_exo)
    print('Playback speed=',100.0,sep="",file=file_exo)
    print('Loop playback=',0,sep="",file=file_exo)
    print('Load alpha channel=',0,sep="",file=file_exo)
    print('file=',path.join(m_path,m_num),sep="",file=file_exo)
    print('[',n+0.1,']',sep="",file=file_exo)
    print('_name=Standard drawing',sep="",file=file_exo)
    print('X=',0.0,sep="",file=file_exo)#Coordinate X,Y,Z
    print('Y=',0.0,sep="",file=file_exo)
    print('Z=',0.0,sep="",file=file_exo)
    print('Expansion rate=',100.00,sep="",file=file_exo)
    print('Transparency=',0.0,sep="",file=file_exo)
    print('rotation=''{:.2f}'.format(0.00),sep="",file=file_exo)
    print('blend=',0,sep="",file=file_exo)
#Voice data
s_flame=1#Beginning frame
e_flame=0#End frame
for m_num in movie_name:
    n=movie_name.index(m_num)#Number of repetitions
    n_v=movie_name.index(m_num)+len(movie_name)#Number of sequential data repetitions+movie_Number of elements in name
    #[]Video order
    print('[',n_v,']',sep="",file=file_exo)#sep delimiter default half-width space
    #Video start-I didn't need end ffmpeg because I did it in the video
    e_flame = int(flame_all[n])+int(e_flame)#Current number of flames+Number of frames at the end of the last time
    print('start=',s_flame,sep="",file=file_exo)
    s_flame=int(e_flame)+1#Number of next start frames=Number of frames at the end of the last time+1
    print('end=',e_flame,sep="",file=file_exo)
    print('layer=',2,sep="",file=file_exo)#Audio file 2
    print('group=',n+1,sep="",file=file_exo)#Remain n
    print('overlay=',1,sep="",file=file_exo)
    print('audio=',1,sep="",file=file_exo)#Deleted the camera instead, which was not in the video here
    print('[''{:.1f}'']'.format(n_v),sep="",file=file_exo)
    print('_name=Audio file',sep="",file=file_exo)#Audio fileではAudio fileにする
    print('Playback position=''{:.2f}'.format(0.00),sep="",file=file_exo)#It was 1 in the video
    print('Playback speed=',100.0,sep="",file=file_exo)
    print('Loop playback=',0,sep="",file=file_exo)
    print('Works with video files=',1,sep="",file=file_exo)
    if n==0 :#It was like this in sample1
        print('file=',path.join(m_path,m_num),sep="",file=file_exo)
    else:
        print('file=',sep="",file=file_exo)
    #print('file=',path.join(m_path,m_num),sep="",file=file_exo)
    print('[',n_v+0.1,']',sep="",file=file_exo)#n->n_v
    print('_name=Standard playback',sep="",file=file_exo)#動画では標準描画 音声 Standard playback
    print('Volume=',100.0,sep="",file=file_exo)
    print('Left and right=',0.0,sep="",file=file_exo)

print(flame_all) 
file_exo.close()

How to use

Set the path where the video is placed in the variable (m_path). After that, if you run it with a command prompt or anaconda, an edit data file (make_exo.exo) for aviutl will be created directly under m_path. If you drag make_exo.exo to the extended timeline of aviutl, the serial numbered videos will be loaded into the timeline.

exo file description

The explanation of the sample data exo file is described below. Since it is a serial number, create a program that outputs information according to the rules of this file. I can't confirm each parameter because I basically just read it from the exo file.

[exedit]
width=908//Video size
height=512//
rate=30//frame rate
scale=1
length=129//Overall final frame
audio_rate=44100
audio_ch=2
[0]//Video file start 0->1->2
start=1 //Start frame
end=79 //End frame
layer=1 
group=1
overlay=1 
camera=0
[0.0] //0.0->1.0->2.0
_name=Video file
Playback position=1
Playback speed=100.0
Loop playback=0
Load alpha channel=0
file=D:\movie\JumpCut_note_DT\0_out_.mp4
[0.1] //0.1->1.1->2.1
_name=Standard drawing
X=0.0
Y=0.0
Z=0.0
Expansion rate=100.00
Transparency=0.0
rotation=0.00
blend=0
[1]
start=80
end=100
layer=1
group=2
overlay=1
camera=0
[1.0]
_name=Video file
Playback position=1
Playback speed=100.0
Loop playback=0
Load alpha channel=0
file=D:\movie\JumpCut_note_DT\1_out_.mp4
[1.1]
_name=Standard drawing
X=0.0
Y=0.0
Z=0.0
Expansion rate=100.00
Transparency=0.0
rotation=0.00
blend=0
[2]//Start audio file
start=1
end=79
layer=2
group=1
overlay=1
audio=1
[2.0]
_name=Audio file
Playback position=0.00
Playback speed=100.0
Loop playback=0
Works with video files=1
file=
[2.1]
_name=Standard playback
Volume=100.0
Left and right=0.0
[3]
start=80
end=100
layer=2
group=2
overlay=1
audio=1
[3.0]
_name=Audio file
Playback position=0.00
Playback speed=100.0
Loop playback=0
Works with video files=1
file=
[3.1]
_name=Standard playback
Volume=100.0
Left and right=0.0

This code explanation

I may write it at a later date. It is output to a text file in the order of the basic exo file.

Finally

This program was created by an amateur of the program just to make it easier. Therefore, I think that there are many cases where the way to write a program is not reached. In that case, I would appreciate it if you could let me know in the comments. Have a good video posting life! !!

Recommended Posts

Import serial number videos together with Aviutl
Connect a large number of videos together!
Web scraping with BeautifulSoup4 (serial number page)
Erase & generate serial number files with shell script
Serial communication with Python
Serial communication with python
Import tsv with Python
Make a gif animation from a serial number file with matplotlib
[Python] Easy reading of serial number image files with OpenCV