📜  trim video linux - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:05:37.820000             🧑  作者: Mango

Trim Video in Linux - Shell Script

As a programmer, you may often come across situations where you need to trim a video in Linux using a shell script. In this post, we will discuss how to write a shell script to trim a video in Linux.

Prerequisites

To trim a video in Linux, you need to have the following tools installed on your system:

  • FFmpeg: A powerful multimedia framework that can decode, encode, transcode, mux, demux, stream, filter, and play multimedia files.
Writing a Shell Script to Trim a Video

The following shell script demonstrates how to trim a video using FFmpeg in Linux:

#!/bin/bash

# Define variables
INPUT_VIDEO=input.mp4
START_TIME="00:00:01"
DURATION="00:00:10"
OUTPUT_VIDEO=output.mp4

# Trim the video
ffmpeg -i $INPUT_VIDEO -ss $START_TIME -t $DURATION -vcodec copy -acodec copy $OUTPUT_VIDEO

echo "Video trimmed and saved as $OUTPUT_VIDEO."

Let's break down the script to understand what each command does:

  1. #!/bin/bash: This line specifies the shell interpreter to use for executing the script.

  2. INPUT_VIDEO=input.mp4: This line defines a variable INPUT_VIDEO and assigns it the value input.mp4.

  3. START_TIME="00:00:01": This line defines a variable START_TIME and assigns it the value 00:00:01, which represents the start time of the trimmed video.

  4. DURATION="00:00:10": This line defines a variable DURATION and assigns it the value 00:00:10, which represents the duration of the trimmed video.

  5. OUTPUT_VIDEO=output.mp4: This line defines a variable OUTPUT_VIDEO and assigns it the value output.mp4, which represents the name of the trimmed video.

  6. ffmpeg -i $INPUT_VIDEO -ss $START_TIME -t $DURATION -vcodec copy -acodec copy $OUTPUT_VIDEO: This line trims the video using FFmpeg. The -i option specifies the input video, -ss option specifies the start time, -t option specifies the duration, -vcodec copy option specifies that the video codec should be copied, and -acodec copy option specifies that the audio codec should be copied. Finally, the $OUTPUT_VIDEO variable specifies the output video.

  7. echo "Video trimmed and saved as $OUTPUT_VIDEO.": This line prints a message indicating that the video has been trimmed and saved.

Conclusion

In this post, we discussed how to write a shell script to trim a video in Linux using FFmpeg. With this script, you can easily trim videos to your desired length and format.