是否可以使用FFmpeg从视频文件夹中剪切“随机”部分并将其合并为1个视频?

时间:2018-11-13 04:29:50

标签: python linux bash video ffmpeg

我意识到这听起来像是一个简单的问题,并且之前已经有人回答过。但是,我似乎找不到能够读取不同长度的视频文件夹,从每个视频中复制随机片段并将其合并为单个视频的脚本。

一个例子:

我有一个文件夹,其中包含150个标记为Fashion-Setlist-01.mp4,Fashion-Setlist-02.mp4等的视频。 每个都超过1小时。我想从每个视频中随机抽取10秒,然后将它们随机添加到一起,从而生成一个视频。仅使用几个视频,这似乎很容易,但是计划是从可能的100多个视频中读取。也应该可以从每个视频中提取多个部分。我想如果视频需要更长的时间,我们可以为更多的片段运行两次脚本。

2 个答案:

答案 0 :(得分:0)

编辑:使用ffmpeg剪切视频需要在关键帧上完成。我对这段代码进行了广泛的编辑,以首先找到关键帧,然后再删除它。它对我有用。

因此,要在bash中进行此操作,假设存在某个程序randtime.py以'H:MM:SS'格式输出一个随机的开始时间,而另一些程序则在附近找到视频关键帧在给定的时间,这是一个快速的hack版本:

#! /usr/bin/env bash

CUTLIST=cut_list.txt
RANDTIME=~/Bin/randtime.py
KEYFRAMER=~/Bin/find_next_key_frame.py

count=0
echo "" > "$CUTLIST"
for file in *.mp4
do
    count=$(( $count + 1 ));
    outfile="cut_$count.mp4"
    start_time=`python "$RANDTIME"`
    # Find the next keyframe, at or after the random time
    start_keyframe_time=`$KEYFRAMER "$file" "$start_time"`
    if [ $? -eq 0 ]; then
        echo "Nearest keyframe to \"$start_time\" is \"$start_keyframe_time\""
        echo "ffmpeg -loglevel quiet -y -i \"$file\" -ss $start_keyframe_time -t 00:00:10 \"$outfile\""
        ffmpeg -loglevel quiet -y -i "$file" -ss $start_keyframe_time -t 00:00:10 "$outfile"
        if [ $? -ne 0 ]; then
            echo "ffmpeg returned an error on [$file], aborting"
        #   exit 1
        fi
        echo "file '$outfile'" >> "$CUTLIST"
    else
        echo "ffprobe found no suitable key-frame near \"$start_time\""
    fi
done

echo "Concatenating ... "
cat "$CUTLIST"

ffmpeg -f concat -i cut_list.txt -c copy all_cuts.mp4

if [ -f "$CUTLIST" ]; then
    rm "$CUTLIST"
fi

随机时间,以python

#! /usr/bin/env python3

import random

#TODO: ensure we're at least 8 seconds before 1 hour

hrs = 0 # random.randint(0,1)
mns = random.randint(0,59)
scs = random.randint(0,59)

print("%d:%02d:%02d" % (hrs,mns,scs))

然后在python中再次精确地找到关键帧,或者恰好在给定时间之后。

#! /usr/bin/env python3

import sys
import subprocess
import os
import os.path

FFPROBE='/usr/bin/ffprobe'

EXE=sys.argv[0].replace('\\','/').split('/')[-1]

if (not os.path.isfile(FFPROBE)):
    sys.stderr.write("%s: The \"ffprobe\" part of FFMPEG seems to be missing\n" % (EXE))
    sys.exit(1)

if (len(sys.argv) == 1 or (len(sys.argv)==2 and sys.argv[1] in ('--help', '-h', '-?', '/?'))):
    sys.stderr.write("%s: Give video filename and time as arguments\n" % (EXE))
    sys.stderr.write("%s:     e.g.: video_file.mp4 0:25:14 \n" % (EXE))
    sys.stderr.write("%s: Outputs the next keyframe at or after the given time\n" % (EXE))
    sys.exit(1)

VIDEO_FILE = sys.argv[1]
FRAME_TIME = sys.argv[2].strip()

if (not os.path.isfile(VIDEO_FILE)):
    sys.stderr.write("%s: The vdeo file \"%s\" seems to be missing\n" % (EXE, VIDEO_FILE))
    sys.exit(1)


### Launch FFMPEG's ffprobe to identify the frames
command = "ffprobe -show_frames -pretty \"%s\"" % VIDEO_FILE
frame_list = subprocess.getoutput(command)

### The list of frames is a huge bunch of lines like:
###    [FRAME]
###    media_type=video
###    key_frame=0
###    best_effort_timestamp=153088
###    best_effort_timestamp_time=0:00:09.966667
###    pkt_duration_time=0:00:00.033333
###    height=360
###    ...
###    [/FRAME]

### Parse the stats about each frame, keeping only the Video Keyframes
key_frames = []
for frame in frame_list.split("[FRAME]"):
    # split the frame lines up into separate "x=y" pairs
    frame_dict = {}
    frame_vars = frame.replace('\r', '\n').replace("\n\n", '\n').split('\n')
    for frame_pair in frame_vars:
        if (frame_pair.find('=') != -1):
            try:
                var,value = frame_pair.split('=', 1)
                frame_dict[var.strip()] = value.strip()
            except:
                sys.stderr.write("%s: Warning: Unable to parse [%s]\n" % (EXE, frame_pair))
        # Do we want to keep this frame?
        # we want video frames, that are key frames
        if ("media_type", "key_frame" in frame_dict and frame_dict["media_type"] == "video" and frame_dict["key_frame"] == "1"):
            key_frames.append(frame_dict)

### Throw away duplicates, ans sort (why are there duplicates?)
key_frame_list = set()
for frame_dict in key_frames:        
    #print(str(frame_dict))
    if ("best_effort_timestamp_time" in frame_dict):
        key_frame_list.add(frame_dict["best_effort_timestamp_time"])
key_frame_list = list(key_frame_list)
key_frame_list.sort()
sys.stderr.write("Keyframes found: %u, from %s -> %s\n" % (len(key_frame_list), key_frame_list[0], key_frame_list[-1]))

### Find the same, or next-larger keyframe
found = False
for frame_time in key_frame_list:
    #sys.stderr.write("COMPARE %s > %s\n" % (frame_time , FRAME_TIME))
    if (frame_time > FRAME_TIME):
        print(frame_time)
        found = True
        break  # THERE CAN BE ONLY ONE!

### Failed?  Print something possibly useful
if (found == False):
    sys.stderr.write("%s: Warning: No keyframe found\n" % (EXE))
    print("0:00:00")
    sys.exit(-1)
else:
    sys.exit(0)  # All'swell

答案 1 :(得分:0)

moviepy是最合适的工具(它使用ffmpeg作为后端)。在moviepy中,串联视频很简单:

import moviepy.editor
import os
import random
import fnmatch 

directory = '/directory/to/videos/'
xdim = 854
ydim = 480
ext = "*mp4"
length = 10

outputs=[]

# compile list of videos
inputs = [os.path.join(directory,f) for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f)) and fnmatch.fnmatch(f, ext)]

for i in inputs:

    # import to moviepy
    clip = moviepy.editor.VideoFileClip(i).resize( (xdim, ydim) ) 

    # select a random time point
    start = round(random.uniform(0,clip.duration-length), 2) 

    # cut a subclip
    out_clip = clip.subclip(start,start+length)

    outputs.append(out_clip)

# combine clips from different videos
collage = moviepy.editor.concatenate_videoclips(outputs) 

collage.write_videofile('out.mp4')