国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

linux中使用ffmpeg 無損剪切/拼接視頻程序

2024-08-27 23:59:29
字體:
來源:轉載
供稿:網友

ffmpeg是一款視頻處理工具了,我們可以在系統中安裝之后利用ffmpeg命令進行視頻的處理了,下面就一起來看看吧.

剪切/拼接視頻文件是一種常見需求,在線視頻網站現在往往將一個視頻文件分割成 n 段,以減少流量消耗,使用 DownloadHelper/DownThemAll 這類工具下載下來的往往就是分割后的文件,能實現剪切/拼接視頻文件的工具多種多樣,但往往都需要進行視頻重編碼(transcoding),這就不可避免的帶來了視頻質量上的損耗,更不用提那長的令人發指的轉換時間了…

其實借助 ffmpeg 我們就可以在不進行視頻重編碼的情況下完成此類任務.

剪切代碼如下:

  1. ffmpeg -i input.mp4 -ss **START_TIME** -t **STOP_TIME** -acodec copy -vcodec copy output.mp4 

其中 START_TIME/STOP_TIME 的格式可以寫成兩種格式:

以秒為單位計數: 80

時:分:秒: 00:01:20

拼接:

拼接的情況稍微復雜些,我們需要將需要拼接的視頻文件按以下格式保存在一個列表 list.txt 中,代碼如下:

  1. file '/path/to/file1' 
  2. file '/path/to/file2' 
  3. file '/path/to/file3' 

相應的命令為如下代碼:

ffmpeg -f concat -i **list.txt** -c copy output.mp4

由于不需要重編碼,這兩條命令幾乎是即時完成的,方便起見,我寫了一個腳本來簡化操作,放在 github 上,請自取,代碼如下:

  1. #!/bin/bash 
  2. #cut/join videos using ffmpeg without quality loss 
  3. if [ -z $1 ] || [ -z $2 ]; then 
  4.    echo "Usage:$0 c[ut] seconds <File>" 
  5.    echo "   eg. $0 c 10 80 example.mp4" 
  6.    echo "   eg. $0 c 00:00:10 00:01:20 example.mp4" 
  7.    echo "Usage:$0 j[oin] <FileType>" 
  8.    echo "   eg. $0 j avi" 
  9.    exit 
  10. fi 
  11. case "$1" in 
  12.    c) 
  13.       echo "cuttig video..." 
  14.       fileName=$(echo $4 | cut -f 1 -d '.'
  15.       fileType=$(echo $4 | cut -f 2 -d '.'
  16.       ffmpeg -i $4 -ss $2 -t $3 -acodec copy -vcodec copy $fileName-$2-$3.$fileType 
  17.       ;; 
  18.    j) 
  19.       echo "joinning videos..." 
  20.       rm temp_list.txt       
  21.       for f in ./*.$2do echo "file '$f'" >> temp_list.txt; done 
  22.       printf "file '%s'\n" ./*.$2 > temp_list.txt 
  23.       ffmpeg -f concat -i temp_list.txt -c copy output.$2 
  24.       rm temp_list.txt  //Vevb.com 
  25.       ;; 
  26.    *) 
  27.       echo "wrong arguments" 
  28.       ;; 
  29. esac 
  30. exit 

以上拼接操作生效的前提是,所有視頻文件的格式編碼相同,如果需要拼接不同格式的視頻文件可以借助以下腳本,代碼如下:

  1. # change this to what you need !!! 
  2. EXTRA_OPTIONS='-vcodec libx264 -crf 23 -preset medium -acodec aac -strict experimental -ac 2 -ar 44100 -ab 128k' 
  3.  
  4. ################################################################################ 
  5. # NO NEED TO TOUCH ANYTHING AFTER THIS LINE! 
  6. ################################################################################ 
  7.  
  8. # the version of the script 
  9. VERSION=1.3 
  10.  
  11. # location of temp folder 
  12. TMP=/tmp 
  13.  
  14. ################################################################################ 
  15.  
  16. echo "MultiMedia Concat Script v$VERSION (mmcat) - A script to concatenate multiple multimedia files." 
  17. echo "Based on FFmpeg - www.ffmpeg.org" 
  18. echo "Don't forget to edit this script and change EXTRA_OPTIONS" 
  19. echo "" 
  20.  
  21. ################################################################################ 
  22. # syntax check (has to have at least 3 params: infile1, infile2, outfile 
  23. ################################################################################ 
  24. if [ -z $3 ]; then 
  25.  echo "Syntax: $0 <input1> <input2> <input3> ... <output>" 
  26.  exit 1 
  27. fi 
  28.  
  29. ################################################################################ 
  30. # get all the command line parameters, except for the last one, which is output 
  31. ################################################################################ 
  32. # $first  - first parameter 
  33. # $last   - last parameter (output file) 
  34. # $inputs - all the inputs, except the first input, because 1st input is 
  35. #           handled separately 
  36. ################################################################################ 
  37. first=${@:1:1} 
  38. last=${@:$#:1} 
  39. len=$(($#-2)) 
  40. inputs=${@:2:$len} 
  41.  
  42. # remove all previous tmp fifos (if exist) 
  43. rm -f $TMP/mcs_* 
  44.  
  45. ################################################################################ 
  46. # decode first input differently, because the video header does not have to be 
  47. # kept for each video input, only the header from the first video is needed 
  48. ################################################################################ 
  49. mkfifo $TMP/mcs_a1 $TMP/mcs_v1 
  50.  
  51. ffmpeg -y -i $first -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a1 2>/dev/null </dev/null & 
  52. ffmpeg -y -i $first -an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_v1 2>/dev/null </dev/null & 
  53.  
  54. # if you need to log the output of decoding processes (usually not necessary) 
  55. # then replace the "2>/dev/null" in 2 lines above with your log file names, like this: 
  56. #ffmpeg -y -i $first -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a1 2>$TMP/log.a.1 </dev/null & 
  57. #ffmpeg -y -i $first -an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_v1 2>$TMP/log.v.1 </dev/null & 
  58.  
  59. ################################################################################ 
  60. # decode all the other inputs, remove first line of video (header) with tail 
  61. # $all_a and $all_v are lists of all a/v fifos, to be used by "cat" later on 
  62. ################################################################################ 
  63. all_a=$TMP/mcs_a1 
  64. all_v=$TMP/mcs_v1 
  65. i=2 
  66. for f in $inputs 
  67. do 
  68.  mkfifo $TMP/mcs_a$i $TMP/mcs_v$i 
  69.  
  70.  ffmpeg -y -i $f -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a$i 2>/dev/null </dev/null & 
  71.  { ffmpeg -y -i $f -an -f yuv4mpegpipe -vcodec rawvideo - 2>/dev/null </dev/null | tail -n +2 > $TMP/mcs_v$i ; } & 
  72.  
  73.  # if you need to log the output of decoding processes (usually not necessary) 
  74.  # then replace the "2>/dev/null" in 2 lines above with your log file names, like this: 
  75.  #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 & 
  76.  #{ ffmpeg -y -i $f -an -f yuv4mpegpipe -vcodec rawvideo - 2>$TMP/log.v.$i </dev/null | tail -n +2 > $TMP/mcs_v$i ; } & 
  77.  
  78.  all_a="$all_a $TMP/mcs_a$i" 
  79.  all_v="$all_v $TMP/mcs_v$i" 
  80.  let i++ 
  81. done 
  82.  
  83. ################################################################################ 
  84. # concatenate all raw audio/video inputs into one audio/video 
  85. ################################################################################ 
  86. mkfifo $TMP/mcs_a_all 
  87. mkfifo $TMP/mcs_v_all 
  88. cat $all_a > $TMP/mcs_a_all & 
  89. cat $all_v > $TMP/mcs_v_all & 
  90.  
  91. ################################################################################ 
  92. # finally, encode the raw concatenated audio/video into something useful 
  93. ################################################################################ 
  94. ffmpeg -f u16le -acodec pcm_s16le -ac 2 -ar 44100 -i $TMP/mcs_a_all \ 
  95.        -f yuv4mpegpipe -vcodec rawvideo -i $TMP/mcs_v_all \ 
  96.  $EXTRA_OPTIONS \ 
  97.  $last 
  98.  
  99. ################################################################################ 
  100. # remove all fifos 
  101. ################################################################################ 
  102. rm -f $TMP/mcs_*

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹东市| 射洪县| 剑河县| 湖州市| 盐山县| 洪江市| 绵竹市| 夏河县| 正安县| 颍上县| 绥宁县| 安图县| 尖扎县| 闵行区| 龙陵县| 嘉峪关市| 师宗县| 永靖县| 扬中市| 黎城县| 博兴县| 阳山县| 长葛市| 黔江区| 灌南县| 尚义县| 怀安县| 黄大仙区| 莫力| 临夏市| 沅陵县| 印江| 恩施市| 科尔| 锡林浩特市| 正宁县| 南投县| 河北省| 万安县| 仪征市| 峨山|