Write a shell script for video processing



Last year, my colleagues planned to build a large lecture hall website within the company, to summarize some internal training videos and provide them to the company colleagues to watch. The website construction is relatively simple and can be directly implemented with Ant.design Pro+Eggjs. But for a better experience, we plan to cut the video according to the content, so that users can quickly find the content they are interested in, and at the same time convert it into m3u8 to upload the CDN to optimize the speed of video playback and buffering.

This involves video cutting and format conversion. The traditional method is to use video editing software to process it, but as a development, I still want to use some automated methods to complete this work. After all, time is the most precious, so I thought of direct Cut, convert and upload with one click using shell scripts.

The first time I wrote a shell script, I also wrote it while learning, so there must be a lot of places that can be optimized, let's learn with you…

video processing ffmpeg

If you want to use the shell to process video, you must first have a command-line tool for processing video. This is not difficult to find. Baidu found ffmpeg, a god-level video processing library.

FFmpeg is the most commonly used open source software for video processing. FFmpeg itself is a huge project with many components and libraries, the most commonly used is its command line tool. This article introduces how the FFmpeg command line processes video, which is more concise and efficient than desktop video processing software. [Borrowing teacher Ruan Yifeng's introduction to ffmpeg]

For an introduction to ffmpeg and video codec, you can go toTeacher Ruan, look here

Our needs are cropping and format conversion, and here are the commands for these two scenarios.


# start time + duration
ffmpeg -ss [start] -i [input] -t [duration] -c copy [output]
# start time + end time
ffmpeg -ss [start] -i [input] -to [end] -c copy [output]
  • -ss: start time of cropping
  • -i: input file
  • -to: cutoff time for cropping
  • -t: length of time
  • -c copy: copy directly without re-encoding

format conversion

ffmpeg -i demo.mp4 -profile:v baseline -level 3.0 
-start_number 0 -hls_time 10 -hls_list_size 0 -f hls output.m3u8
  • -profile:v baseline: The output encoding specifies the specific H.264 standard.
  • -level:–
  • -start_number:–
  • -hls_time: time per slice
  • -hls_list_size: Set the maximum number of items saved in the playlist, set to 0 to save all the information
  • -f: specify the video container

Configuration parameters

sed command

In order to facilitate the processing of different videos later, I hope to extract some configuration parameters, the configuration fileconfigas follows:

File=source.mp4 # The address of the file to be processed
Cut_config=videos.csv # Timestamp data
Output_path=./output/ # The output path of the video file after cropping and converting
Qiniu_prefix=videos/source/ # Upload the path of Qiniu

The read script is as follows

file=`sed '/^File=/!d;s/.*=//' config`  
cutConfig=`sed '/^Cut_config=/!d;s/.*=//' config`  
outputPath=`sed '/^Output_path=/!d;s/.*=//' config`  
qiniuPrefix=`sed '/^Qiniu_prefix=/!d;s/.*=//' config` 

The sed command is used here, and sed can process and edit text files according to the instructions of the script. Here, we mainly read the key-value pair type data in the configuration file.

sed '/^File=/!d;s/.*=//' config: turn upFile=line starting with!dDeleting lines other than this one, will=and= character beforeis replaced with an empty string, and what is returned is=the following string.

Read time slice csv

Video slices need to know the start and end time and the name of the video after cutting. This data organization method is more suitable for csv, so we put it in csv for management, and each row represents a video clip.


The next step is to parse the csv file.

# variable declaration
count=0 # count
videos=() # video list

# traverse
for line in $(cat $cutConfig)
    IFS="," # Use, to cut the string
    arr=($line) # After cutting, get an array of three strings
    IFS="$OLD_IFS" # restore IFS
    videos[$count]="$outputPath${arr[2]}" # Store the filename of the clipped video clip
    # ffmpeg crop file
    ffmpeg -ss ${arr[0]} -t ${arr[1]} -i $file -vcodec copy -acodec copy $outputPath${arr[2]}.mp4 -loglevel quiet 

Convert video formats

# Convert video format
for video in ${videos[@]}
    ffmpeg -i $video.mp4 -profile:v baseline -level 3.0  -start_number 0 -hls_time 10 -hls_list_size 0 -f hls $video.m3u8

upload qiniu

# upload video
qshell qupload2 --src-dir=$outputPath --bucket=assets --key-prefix=$qiniuPrefix --skip-suffixes=mp4 --overwrite=true --rescan-local=true


To get the time switching of the video, simply use the video tag to write an interface, drag and drop to quickly or remove the start and end time, and then export a CSV file in a specified format.


So far, this article about shell script to realize video processing is introduced here. For more relevant shell script video processing content, please search for previous articles of developpaer or continue to browse the related articles below. I hope you will support developpaer more in the future!

Recommended Today

One command to configure Vim as a powerful IDE

One command canVimConfigured as a powerfulC/C++ IDE. Including installation is not easyYouCompleteMeThe plug-in is also automatically installed, and the latest version will be automatically downloaded from the official websitelibclang, and then compileYouCompleteMerequired by the pluginycm_core library, which is probably installed so farYouCompleteMeSimplest pose for plugins. Install: curl -o – https://raw.githubusercontent.com/HmyBmny/vimrc/master/install-vim-plugins | sh The use of […]