Task: In order to advertise a company, I have to download a bunch of(1000 or so) videos related to the company from Youku, then add title sequence and watermark(or say subtitles), finally upload all of them to Youku.
I’ve tried Adobe Premiere, OpenCV, and finally use ffmpeg and python to accomplish this task.
Here is how I hacked the task.
Download Videos
- Find the video I want to download.
- Use flvcd to get the downloadable link.
- Download it.
I have to do 1 to 3 in loop, so I write python script to do this.
1 | import urllib2 |
Edit videos
Adobe Premiere
Adobe Premiere is one of the most powerful video edit tools. It works very well on generating single output. However, we have to deal with a thousand videos repeatedly, it would be time consuming.
Computer does repeated things better than human, so I try to write lines of program to do that, and here is my attempts:
OpenCV
OpenCV is a library of programming functions mainly aimed at real-time computer vision and it contain a video session. However it deal with video well but it save without audio, so it cannot work in this task.
ffmpeg
Later, I found ffmpeg, which perfectly suit this task.
This is ffmpeg script running in terminel:
ffmpeg [global_options] {[input_file_options] -i input_file} ... {[output_file_options] output_file} ...
Here I list the commends I used in edit videos.
change the size of a video:
ffmpeg -i input.mkv -s 512x288 output.mkv
concatenate two videos(the two videos should have the same size, SAR and so on)
ffmpeg -i head.mp4 -i output.mkv -filter_complex "[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output-cat.mkv
add watermarks:
ffmpeg -i output-cat.mp4 -itsoffset 00:00:05 -t 00:00:08 -i title.png -filter_complex '[1:v]scale=400:100[s];[0:v][s]overlay=0:main_h-overlay_h' output-mark.avi
the time after -itsoffset denotes when the mark star to appear, the time after -t is suppose to denote the duration, but it doesn’t work for picture; size of mark(scale = 400:100); position(overlay=10:10), 0:0 denotes leftup, and main_w-overlay_w:main_h-overlay_h denotes right button.
Since watermark cannot appear for a while and hide for a while, I need other way to present the advertisement, and here comes the subtitle.
add subtitle:
ffmpeg -i output-cat.mp4 -i test.srt -map 0:0 -scodec copy -map 1:0 -vcodec copy -map 1:1 -acodec copy output-sub.avi
python again
All of the commend works on single output, so I turn to python
1 | import os |
Here we should have done the task. However, the video I downloaded are in various size and SAR. It seems has no function to ajust SAR directly, so this program has some flaws.
Finally I find what I was doing is recreate wheel, that is there is already a robust software can do this task. 那个软件叫“音影转霸”,可以在这里下载。