更短的命令

更短的命令

我使用的是 Arch Linux 4.13.10,我想合併.MOV多個用佳能 EOS 相機拍攝的檔案。

我嘗試使用 FFmpeg 將文件轉換為傳輸流(.ts文件),如圖所示這裡,但生成的文件中缺少聲音。

我希望產生的文件是.mp4,但這不是嚴格要求的。

我該怎麼做呢?

答案1

我成功地使用合併文件FFmpeg 的解復用功能。對於.mp4轉換,我必須明確轉換音訊串流以避免此錯誤:

在串流 #1 中找不到編解碼器 pcm_s16le 的標籤,容器目前不支援編解碼器

這是將文件組合到的命令merged.mp4

ffmpeg -f concat -i files_to_combine.txt -vcodec copy -acodec aac -strict -2 -b:a 384k merged.mp4

如果輸出文件也可以是.MOV文件,則指令為:

ffmpeg -f concat -i files_to_combine.txt -vcodec copy -acodec copy merged.MOV

這是文字檔案的內容files_to_combine.txt

file ./first_file.MOV
file ./second_file.MOV

如果出現錯誤

不安全的檔案名稱「有空格的檔案.mov」files_to_combine.txt:不允許操作

並信任文件名,您可以添加-safe 0來解決此錯誤。請參閱 FFmpeg 的手冊頁:

ffmpeg -safe 0 -f concat ...

更短的命令

五年後嘗試同樣的任務,我也取得了成功

ffmpeg -f concat -i files_to_combine.txt merged.mov

ffmpeg -f concat -i files_to_combine.txt -f mp4 merged.mp4

答案2

find *.MOV | sed 's:\ :\\\ :g'| sed 's/^/file /' > fl.txt; ffmpeg -f concat -i fl.txt -c copy output.mp4; rm fl.txt

答案3

如果您正在尋找如何連接.mp4文件,請參閱我的其他答案:Stack Overflow:如何使用 FFmpeg 連接兩個 MP4 檔案?

將多個文件合併.mov.MOV一個

……這是您需要做的:

# 0. Copy all .mov files that (you'd like to combine) into a single directory. 
# Then `cd` into that directory. 

# 1. Create a "files.txt" file containing a list of files to combine
find . -type f -iname "*.mov" | sort -V \
    | awk '{print "file \x27" $0 "\x27"}' > files.txt

# [WHAT I USUALLY USE: FAST]
# 2. **If all of your .mov files have the same encoding** (ex: they were 
# all recorded on the same video camera and with the same settings), you can
# specify to use the "copy" codec via `-c copy`, which means it will NOT 
# have to re-encode the video. This just concatenates all the files together. 
# THIS TAKES **SECONDS**. 
time ffmpeg -f concat -safe 0 -i files.txt -c copy merged.mov 

# [SLOW; SOMETIMES NECESSARY]
# OR 2. **If each of your .mov files has a different encoding,** you'll need to 
# re-encode them all into a single stream. 
# - This uses the "files.txt" file created above, and outputs the merged 
#   file into "merged.mov". 
# THIS COULD TAKE **HOURS**. 
time ffmpeg -f concat -safe 0 -i files.txt merged.mov

我通常處於可以使用第一個選項的情況,該選項需要代替小時,這太棒了!

額外的細節和解釋

  • 如果ls -1顯示您有這些文件:

    REC_0009.MOV
    REC_0010.MOV
    REC_0011.MOV
    

    然後該find命令將生成此files.txt文件:

    file './REC_0009.MOV'
    file './REC_0010.MOV'
    file './REC_0011.MOV'
    
  • -iname "*.mov"部分做了一個案例——senstive* 符合所有以.mov.MOV或任何其他大寫字母組合結尾的檔案。

  • 如果您忘記了-safe 0命令ffmpeg,並且任何檔案名稱中都有偶數形式的「目錄」./,您將收到以下錯誤:

    [concat @ 0x555a24968700] Unsafe file name './REC_0009.MOV'
    files.txt: Operation not permitted
    

    -safe 0選項指示ffmpeg忽略此錯誤並接受檔案路徑中的“不安全”目錄。

    @Matthias Braun 在這裡提到了這一點。

    ffmpeg手冊頁僅限線上不是在我的 Linux Ubuntu 20.04 電腦本地的手冊頁中man ffmpeg):https://manpages.org/ffmpeg:

    safe

    如果設定為1,則拒絕不安全的檔案路徑。如果檔案路徑不包含協定規範且是相對的,且所有元件僅包含可移植字元集中的字元(字母、數字、句點、下劃線和連字號)且元件開頭沒有句點,則該檔案路徑被認為是安全的。

    如果設定為0,則接受任何檔案名稱。

    預設為1.

    -1相當於1自動探測格式,0否則。

  • -c copy選項指示ffmpeg使用“複製”編解碼器,這意味著它不會重新編碼視訊。這就是它如此之快的原因。

參考

  1. 我學@Matthias Braun 也有回答
  2. 我在這裡的回答是我用來\x27製作'角色的:Stack Overflow:如何轉義單引號字串中的單引號
  3. 我在 VSCode 文字編輯器中與 GitHub Copilot AI 進行了很多討論,同時弄清楚了上述命令的細微差別。
  4. 我對 MP4 文件的回答:Stack Overflow:如何使用 FFmpeg 連接兩個 MP4 檔案?

相關內容