Segment video

1. Problem

I need to segment a long video into some short video clips according to the time stamps I specify.

2. Code

#!/usr/bin/env python

'''
===========
Split Video
-----
Usage: ./split.py [video file name] [time file name]
'''

import os

def splitVideo(videofile, timefile):
    f = open(timefile, "r")
    cnt = 0
    while True:
        line = f.readline().strip('\n')
        if line:
            start, end = line.split(" ")
            os.system('ffmpeg -i ' + videofile + ' -ss ' + str(start) + ' -vcodec copy -acodec copy -to ' + str(end) + ' ' + str(cnt) +'.mp4')
            cnt = cnt + 1
        else:
            break
    f.close()

if __name__ == '__main__':
    print __doc__

    import sys
    try:
        video = sys.argv[1]
        time = sys.argv[2]
        splitVideo(video, time)
    except:
        print "Please set input file."

3. Time Stamps

time.txt like this:

0 12
19 23
29 50
1:20 1:30
...

4. Feelings

  1. Python is an excellent tool as an system script language.
  2. I tried to code in shell, which is also an excellent script tool. But I failed to be familiar with its grammar and got lost in shell. This is the shell code I write:
#!/bin/bash

declare -i cnt
cnt=0
startTime=(0,0,0,0,0,0,0)
endTime=(0,0,0,0,0,0,0)
cat time.txt | while read line
do
    begin=`echo $line | cut -d ' ' -f 1`
    end=`echo $line | cut -d ' ' -f 2`
    startTime[$cnt]=$begin
    endTime[$cnt]=$end
    echo $line
    echo $begin,$end,$cnt
    #echo "ffmpeg -ss ${begin} -i test00.mp4 -vcodec copy -acodec copy -t ${end} ${cnt}.mp4"
    ffmpeg -ss ${begin} -i test00.mp4 -vcodec copy -acodec copy -t ${end} ${cnt}.mp4
    cnt=$cnt+1
done

Shell code looks a little wired. Since it is more like a tool than a language.

The problem is, there is so mush powerful tools in linux, which can be used directly and freely in shell. Those amazing tools really attract me.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值