python和C语言视频处理

python

python

import os

#视频处理

def file_name(path_file):

FileName = os.path.basename(path_file)#带后缀的文件名(不含路径)

FileName_NoExtension = FileName.split(\'.\')[0]#不带后缀的文件名(此方法不适用于文件名含多个点号的文件)

Extension_Name = os.path.splitext(path_file)[-1]#后缀

return FileName

class video():

def __init__(self):

import subprocess

print(\'(1)视频合并\n(2)视频剪辑\n(3)视频格式转换\n(4)提取音频\n(5)音视频合并\n(6)退出\')

b = eval(input(\'请选择:\n\'))

flag = input(\'是否只执行此项任务? yes or no\n\')

while True:

if(b==1):

self.video_convert()

elif(b==2):

self.video_clip()

elif(b==3):

self.video_format_conversion()

elif(b==4):

print(\'请输入视频的存储路径:\')

video = input(r\'\')

cmd = \'ffmpeg -i {} -f mp3 -vn {}\'.format(video, video+\'.mp3\')

subprocess.call(cmd,shell=True)

print(\'提取完成!\')

elif(b==5):

self.video_audio_merge()

elif(b==6):

break

else:

print(\'输入有误!\')

break

if (flag==\'no\' or flag==\'NO\'):

print(\'(1)视频合并\n(2)视频剪辑\n(3)视频格式转换\n(4)提取音频\n(5)音视频合并\n(6)退出\')

b = eval(input(\'请选择:\n\'))

#视频合并

def video_convert(self):

import subprocess

print(\'请输入视频个数:\')

num = eval(input())

cmd = \'ffmpeg -i "concat:\'

for i in range(num):

print(\'请输入视频%d的存储路径:\'%(i+1))

a = input(r\'\')

cmd = cmd + \'{}|\'.format(a)

cmd = cmd + \'" -c copy {}\'.format(a.replace(\'.mp4\',\'_merge.mp4\'))

subprocess.call(cmd,shell=True)

print(\'合并完成!\')

#\'ffmpeg -i "concat:{}|{}|{}|" -c copy {}\'

#视频格式转换

def time(self,file_path):

import cv2

cap = cv2.VideoCapture(file_path)

# get方法参数按顺序对应下表(从0开始编号)

rate = cap.get(5) # 帧速率

FrameNumber = cap.get(7) # 视频文件的帧数

duration = FrameNumber/rate/60 # 帧速率/视频总帧数 是时间,除以60之后单位是分钟

minutes = int(duration)

seconds = int((duration - minutes) * 60)

return str(minutes) + \':\' + str(seconds)

def video_format_conversion(self):

import subprocess

while True:

print(\'请输入视频路径:\')

video = input()

#print(\'请输入需要转换的视频格式:\')

style = \'mp4\'#input()

time = self.time(video)

cmd = \'ffmpeg -i {} -ss 00:00:00 -c copy -to {} {}\'.format(video,time,file_name(video))

subprocess.call(cmd,shell=True)

print(\'转换完成!\')

#视频剪辑

def video_clip(self):

import subprocess

print(\'请输入视频路径:\')

video = input()

print(\'请输入开始时间:\')

start = input()

print(\'请输入结束时间:\')

end = input()

cmd = \'ffmpeg -i {} -ss {} -c copy -to {} {}\'.format(video,start,end,video.replace(\'.mp4\',\'_clip.mp4\'))

subprocess.call(cmd,shell=True)

print(\'剪辑完成!\')

#音视频合并

def video_audio_merge(self):

import subprocess

print("请输入视频文件存放路径:")

video = input(r\'\')

print("请输入音频文件存放路径:")

audio = input(r\'\')

cmd = \'ffmpeg -i {} -i {} -c:v copy -c:a aac -strict experimental {}\'.format(video,audio,video.replace(\'.mp4\',\'_merge.mp4\'))

subprocess.call(cmd,shell=True)

print(\'合并完成!\')

if __name__ == \'__main__\':

video()

 

C语言:

#include<stdio.h>

#include<string.h>

char* get_name(char* path, int choice){

char pure_file_name[100],file_name[100],extension_name[100];

int i,j,flag = 0;

int len = strlen(path); //取字符串长度

////////////////////////获取后缀名 //////////////////

for(i=len-1; i>=0; i--)

if(path[i] == \'.\'){ //找到最后一个点号

strcpy(extension_name,&path[i+1]);//获取后缀名

break;

}

/////////////////////获取文件名(不含路径,含后缀)////////////////////////

for(i=len-1; i>=0; i--)

if(path[i] == \'\\\'){ //找到最后一个斜杠

strcpy(file_name,&path[i+1]);//获取文件名(不含路径,含后缀)

break;

}

/////////////////////////获取纯文件名(不含路径和后缀)////////////////////////////////

for(i=len-1; i>=0; i--){

if(path[i] == \'.\' && !flag){

j = i; //记录最后一个点号位置

flag = 1;

}

if(path[i] == \'\\\'){ //找到最后一个斜杠

strncpy(pure_file_name,path+i+1,j-i-1);//获取纯文件名(不含路径和后缀)

break;

}

}

char* result;

switch(choice){

case 1:

strcpy(result,file_name);//文件名(不含路径,含后缀)

break;

case 2:

strcpy(result,pure_file_name);//纯文件名(不含路径和后缀)

break;

case 3:

strcpy(result,extension_name);//后缀

break;

default:

return "choice error!";

}

return result;

}

char* str_replace(char* str,char* oldstr,char* newstr){

char bstr[strlen(str)*2];//转换缓冲区

int i;

memset(bstr,0,sizeof(bstr));

for(i=0; i<strlen(str); i++){

if(!strncmp(str+i,oldstr,strlen(oldstr))){//查找目标字符串

strcat(bstr,newstr);

i = i + strlen(oldstr) - 1;

}else{

strncat(bstr,str+i,1);//保存一字节进缓冲区

}

}

char tmp[strlen(str)*2];

strcpy(tmp,bstr);

return tmp;

}

//视频剪辑

void video_clip(){

char video[50],start[50],end[50],cmd[500];

printf("请输入视频路径:\n");

scanf("%s",video);

printf("请输入开始时间:\n");

scanf("%s",start);

printf("请输入结束时间:\n");

scanf("%s",end);

str_replace(video,".mp4","_clip.mp4");

//cmd = \'ffmpeg -i {} -ss {} -c copy -to {} {}\'.format(video,start,end,video.replace(\'.mp4\',\'_clip.mp4\'))

//sprintf(cmd, "%s%s%s%s%s%s%s%s", "ffmpeg -i ", video, " -ss ", start, " -c copy -to ", end, " ", video);

sprintf(cmd,"ffmpeg -i %s -ss %s -c copy -to %s %s",video,start,end,str_replace(video,".mp4","_clip.mp4"));

system(cmd);

printf("剪辑完成!\n");

}

//音视频合并

void video_audio_merge(){

char video[50],audio[50],cmd[100];

printf("请输入视频文件存放路径:\n");

scanf("%s",video);

printf("请输入音频文件存放路径:\n");

scanf("%s",audio);

str_replace(video,".mp4","_merge.mp4");

sprintf(cmd,"ffmpeg -i %s -i %s -c:v copy -c:a aac -strict experimental %s",video,audio,str_replace(video,".mp4","_merge.mp4"));

system(cmd);

printf("合并完成!\n");

}

//视频合并

void video_convert(){

int num,i;

printf("请输入视频个数:");

scanf("%d",&num);

char cmd[500] = "ffmpeg -i \"concat:";

char a[50];

for(i=0; i<num; i++){

printf("请输入视频%d的存储路径:",i+1);

scanf("%s",a);

sprintf(cmd, "%s%s%s", cmd, a, "|");

}

//\'ffmpeg -i "concat:{}|{}|{}|" -c copy {}\';

sprintf(cmd, "%s%s%s", cmd, "\" -c copy ", str_replace(a,".mp4","_merge.mp4"));

system(cmd);

printf("合并完成!");

}

int main(){

void video_clip();

void video_audio_merge();

void video_convert();

char* str_replace(char* str,char* oldstr,char* newstr);

char* get_name(char* path, int choice);

printf("(1)视频合并\n(2)视频剪辑\n(3)提取音频\n(4)音视频合并\n(5)退出\n请选择:");

int b,exit=0;

scanf("%d",&b);

char flag[10];

printf("是否只执行此项任务? yes or no\n");

scanf("%s",flag);

while(!exit){

switch (b){

case 1:

video_convert();

break;

case 2:

video_clip();

break;

case 3:

printf("请输入视频的存储路径:\n");

char video[100];

scanf("%s",video);

char cmd[100] = "ffmpeg -i ";

char audio[100];

strcpy(audio, str_replace(video,".mp4",".mp3"));

sprintf(cmd, "ffmpeg -i %s -f mp3 -vn %s", video, audio);

system(cmd);

printf("提取完成!\n");

break;

case 4:

video_audio_merge();

break;

case 5:

exit = 1;

break;

default:

printf("Error:输入有误!\n");

break;

}

if (flag=="no" || flag=="NO"){

printf("(1)视频合并\n(2)视频剪辑\n(3)提取音频\n(4)音视频合并\n(5)退出\n请选择:\n");

scanf("%d",&b);

}

}

}

 

import os

#视频处理

def file_name(path_file):

    FileName = os.path.basename(path_file)#带后缀的文件名(不含路径)

    FileName_NoExtension = FileName.split(\'.\')[0]#不带后缀的文件名(此方法不适用于文件名含多个点号的文件)

    Extension_Name = os.path.splitext(path_file)[-1]#后缀

    return FileName

class video():

    def __init__(self):

        import subprocess

        print(\'(1)视频合并\n(2)视频剪辑\n(3)视频格式转换\n(4)提取音频\n(5)音视频合并\n(6)退出\')

        b = eval(input(\'请选择:\n\'))

        flag = input(\'是否只执行此项任务? yes or no\n\')


        while True:

            if(b==1):

                self.video_convert()

            elif(b==2):

                self.video_clip()

            elif(b==3):

                self.video_format_conversion()

            elif(b==4):

                print(\'请输入视频的存储路径:\')

                video = input(r\'\')

                cmd = \'ffmpeg -i {} -f mp3 -vn {}\'.format(video, video+\'.mp3\')

                subprocess.call(cmd,shell=True)

                print(\'提取完成!\')

            elif(b==5):

                self.video_audio_merge()

            elif(b==6):

                break

            else:

                print(\'输入有误!\')

                break  

            if (flag==\'no\' or flag==\'NO\'):

                print(\'(1)视频合并\n(2)视频剪辑\n(3)视频格式转换\n(4)提取音频\n(5)音视频合并\n(6)退出\')

                b = eval(input(\'请选择:\n\'))


    #视频合并

    def video_convert(self):

        import subprocess

        print(\'请输入视频个数:\')

        num = eval(input())

        cmd = \'ffmpeg -i "concat:\'  

        for i in range(num):

            print(\'请输入视频%d的存储路径:\'%(i+1))

            a = input(r\'\')

            cmd = cmd + \'{}|\'.format(a)

        cmd = cmd +  \'" -c copy {}\'.format(a.replace(\'.mp4\',\'_merge.mp4\'))

        subprocess.call(cmd,shell=True)

        print(\'合并完成!\')

        #\'ffmpeg -i "concat:{}|{}|{}|" -c copy {}\'

    #视频格式转换

    def time(self,file_path):

        import cv2

        cap = cv2.VideoCapture(file_path)

        # get方法参数按顺序对应下表(从0开始编号)

        rate = cap.get(5)   # 帧速率

        FrameNumber = cap.get(7)  # 视频文件的帧数

        duration = FrameNumber/rate/60  # 帧速率/视频总帧数 是时间,除以60之后单位是分钟

        minutes = int(duration)

        seconds = int((duration - minutes) * 60)

        return str(minutes) + \':\' + str(seconds)

    def video_format_conversion(self):

        import subprocess

        while True:

            print(\'请输入视频路径:\')

            video = input()

            #print(\'请输入需要转换的视频格式:\')

            style = \'mp4\'#input()

            time = self.time(video)

            cmd = \'ffmpeg -i {} -ss 00:00:00 -c copy -to {}{}\'.format(video,time,file_name(video))

            subprocess.call(cmd,shell=True)

            print(\'转换完成!\')

    #视频剪辑

    def video_clip(self):

        import subprocess

        print(\'请输入视频路径:\')

        video = input()

        print(\'请输入开始时间:\')

        start = input()

        print(\'请输入结束时间:\')

        end = input()

        cmd = \'ffmpeg -i {} -ss {} -c copy -to {}{}\'.format(video,start,end,video.replace(\'.mp4\',\'_clip.mp4\'))

        subprocess.call(cmd,shell=True)

        print(\'剪辑完成!\')

    #音视频合并

    def video_audio_merge(self):

        import subprocess

        print("请输入视频文件存放路径:")

        video = input(r\'\')

        print("请输入音频文件存放路径:")

        audio = input(r\'\')

        cmd = \'ffmpeg -i {} -i {} -c:v copy -c:a aac -strict experimental {}\'.format(video,audio,video.replace(\'.mp4\',\'_merge.mp4\'))

        subprocess.call(cmd,shell=True)

        print(\'合并完成!\')


if __name__ == \'__main__\':

    video()

以上是 python和C语言视频处理 的全部内容, 来源链接: utcz.com/z/386502.html

回到顶部