FFmpeg Lossless cut/Splice video program used in Linux

Source: Internet
Author: User
Tags decode all

Cutting/stitching video files is a common requirement. Online video sites now tend to split a video file into n segments to reduce traffic consumption. The use of downloadhelper/downthemall such tools to download is often the split file. can achieve the cutting/splicing of video files of various tools, but often need to do video transcoding, which inevitably brought the loss of video quality, not to mention that long heinous conversion time ...

In fact, with the help of ffmpeg, we can accomplish such tasks without video recoding:

Shear:

The code is as follows Copy Code
Ffmpeg-i input.mp4-ss **start_time**-t **stop_time**-acodec copy-vcodec copy Output.mp4

The Start_time/stop_time format can be written in two different formats:

Count in seconds: 80
Time: minutes: sec: 00:01:20
Stitching:

The stitching is a little more complicated and we need to keep the video files that need stitching in the following format in a list list.txt:

The code is as follows Copy Code
File '/path/to/file1 '
File '/path/to/file2 '
File '/path/to/file3 '

The corresponding commands are:

The code is as follows Copy Code
Ffmpeg-f concat-i **list.txt**-C copy Output.mp4

Since no recoding is required, these two commands are almost instantaneous.

For convenience, I wrote a script to simplify the operation. Put on the GitHub, please yourself:

The code is as follows Copy Code

#!/bin/bash
#cut/join videos using ffmpeg without quality loss

If [-Z $] | | [-Z $]; Then
echo "usage:$0 C[ut] seconds < File> "
echo "eg. $ C Example.mp4 "
echo "eg. $ c 00:00:10 00:01:20 Example.mp4 "
echo "Usage:$0 J[oin] < Filetype> "
echo "eg. $ J avi "
Exit
Fi

Case "$" in
C
echo "Cuttig video ..."
filename=$ (echo $ | cut-f 1-d '. ')
filetype=$ (echo $ | cut-f 2-d '. ')
Ffmpeg-i $4-ss $2-t $3-acodec copy-vcodec copy $fileName-$2-$3. $fileType
;;
J
echo "joinning videos ..."
RM temp_list.txt
For f in./*.$2; Do echo "file ' $f '" >> Temp_list.txt; Done
printf "file '%s ' \ n"./*.$2 > Temp_list.txt
Ffmpeg-f concat-i temp_list.txt-c Copy output.$2
RM temp_list.txt
;;
*)
echo "Wrong arguments"
;;
Esac
Exit

The above splicing operation is valid on the premise that all video files have the same format encoding, if you need to splice different formats of video files can use the following script

The code is as follows Copy Code

# change this to what your need!!!
Extra_options= '-vcodec libx264-crf 23-preset medium-acodec aac-strict experimental-ac 2-ar 44100-ab 128k '

################################################################################
#
# NO NEED to touch anything on this line!
#
################################################################################

# The version of the script
version=1.3

# Location of temp folder
Tmp=/tmp

################################################################################

echo "Multimedia Concat script v$version (mmcat)-A script to concatenate multiple multimedia files."
echo "Based on ffmpeg-www.ffmpeg.org"
echo "Don ' t forget to edit the script and change Extra_options"
echo ""

################################################################################
# syntax Check (has to have at least 3 params:infile1, Infile2, outfile
################################################################################
If [-Z $]; Then
echo "Syntax: $ <input1> <input2> <input3> ... <output>"
Exit 1
Fi

################################################################################
# Get all of the command line parameters, except for the last one, which is output
################################################################################
# $first-first parameter
# $last-last parameter (output file)
# $inputs-all the inputs, except the ' the ' I input, because 1st input is
# handled separately
################################################################################
First=${@:1:1}
Last=${@:$#:1}
len=$ (($#-2))
Inputs=${@:2: $len}

# Remove all previous TMP FIFOs (if exist)
Rm-f $TMP/mcs_*

################################################################################
# decode-differently, because the video header does not have to IS
# kept for each video input, only the header from the ' the ' ' the ' ' needed
################################################################################
Mkfifo $TMP/mcs_a1 $TMP/mcs_v1

Ffmpeg-y-I $first-vn-f u16le-acodec pcm_s16le-ac 2-ar 44100 $TMP/mcs_a1 2>/dev/null </dev/null &
Ffmpeg-y-I $first-an-f yuv4mpegpipe-vcodec rawvideo $TMP/mcs_v1 2>/dev/null </dev/null &

# If you are need to log the output of decoding processes (usually not necessary)
# then replace the ' 2>/dev/null ' in ' 2 lines above with your log file names as this:
#ffmpeg-I $first-vn-f u16le-acodec pcm_s16le-ac 2-ar 44100 $TMP/mcs_a1 2> $TMP/log.a.1 </dev/null &
#ffmpeg-I $first-an-f yuv4mpegpipe-vcodec rawvideo $TMP/mcs_v1 2> $TMP/log.v.1 </dev/null &

################################################################################
# decode all the other inputs, remove the-a-a-header with tail
# $all _a and $all _v are lists of all/A/A/A/V FIFOs, to is used by ' cat ' later on
################################################################################
All_a= $TMP/MCS_A1
all_v= $TMP/MCS_V1
i=2
For F in $inputs
Todo
Mkfifo $TMP/mcs_a$i $TMP/mcs_v$i

Ffmpeg-y-I $f-vn-f u16le-acodec pcm_s16le-ac 2-ar 44100 $TMP/mcs_a$i 2>/dev/null </dev/null &
{ffmpeg-y-i $f-an-f yuv4mpegpipe-vcodec rawvideo-2>/dev/null </dev/null | tail-n +2 > $TMP/mcs_v$i;} &

# If you are need to log the output of decoding processes (usually not necessary)
# then replace the ' 2>/dev/null ' in ' 2 lines above with your log file names as this:
#ffmpeg-y-i $f-vn-f u16le-acodec pcm_s16le-ac 2-ar 44100 $TMP/mcs_a$i 2> $TMP/log.a. $i </dev/null &
#{ffmpeg-y-I $f-an-f yuv4mpegpipe-vcodec rawvideo-2> $TMP/log.v. $i </dev/null | tail-n +2 > $TMP/mcs_v$i ; } &

All_a= "$all _a $TMP/mcs_a$i"
all_v= "$all _v $TMP/mcs_v$i"
Let i++
Done

################################################################################
# concatenate all raw audio/video inputs into one audio/video
################################################################################
Mkfifo $TMP/mcs_a_all
Mkfifo $TMP/mcs_v_all
Cat $all _a > $TMP/mcs_a_all &
Cat $all _v > $TMP/mcs_v_all &

################################################################################
# Finally, encode the raw concatenated audio/video into something useful
################################################################################
Ffmpeg-f u16le-acodec pcm_s16le-ac 2-ar 44100-i $TMP/mcs_a_all\
-F yuv4mpegpipe-vcodec rawvideo-i $TMP/mcs_v_all\
$EXTRA _options\
$last

################################################################################
# Remove All FIFOs
################################################################################
Rm-f $TMP/mcs_*

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.