音訊與視訊處理技能 是做什麼的?
完整的音訊與視訊處理指南。
快速參考
FFmpeg 命令
| 任務 | 命令 |
|---|---|
| 轉換 | ffmpeg -i input.mp4 output.webm |
| 萃取音訊 | ffmpeg -i video.mp4 -vn audio.mp3 |
| 縮圖 | ffmpeg -i video.mp4 -ss 5 -frames:v 1 thumb.jpg |
| 調整尺寸 | ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4 |
| 壓縮 | ffmpeg -i input.mp4 -crf 28 output.mp4 |
常用格式
影片:MP4、WebM、MKV、AVI、MOV
音訊:MP3、AAC、WAV、FLAC、OGG
編碼器:H.264、H.265/HEVC、VP9、AV1
1. FFmpeg 基礎
安裝
# Ubuntu/Debian
sudo apt install ffmpeg
# macOS
brew install ffmpeg
# Windows (Chocolatey)
choco install ffmpeg
基本轉換
# 影片格式轉換
ffmpeg -i input.avi output.mp4
# 音訊格式轉換
ffmpeg -i input.wav output.mp3
# 指定編碼器
ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4
影片品質控制
# CRF(固定位元速率因子)- 0-51,數值越低品質越好
ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
# 位元速率控制
ffmpeg -i input.mp4 -c:v libx264 -b:v 2M output.mp4
# 雙遍編碼以獲得更好品質
ffmpeg -i input.mp4 -c:v libx264 -b:v 2M -pass 1 -f null /dev/null
ffmpeg -i input.mp4 -c:v libx264 -b:v 2M -pass 2 output.mp4
2. 影片處理
調整尺寸與縮放
# 縮放到指定解析度
ffmpeg -i input.mp4 -vf "scale=1920:1080" output.mp4
# 保持比例縮放
ffmpeg -i input.mp4 -vf "scale=1280:-1" output.mp4 # 自動高度
ffmpeg -i input.mp4 -vf "scale=-1:720" output.mp4 # 自動寬度
# 帶填充的縮放(信箱模式)
ffmpeg -i input.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2" output.mp4
修剪與裁剪
# 從時間戳到時間戳裁剪
ffmpeg -i input.mp4 -ss 00:01:00 -to 00:02:00 -c copy output.mp4
# 按持續時間裁剪
ffmpeg -i input.mp4 -ss 00:01:00 -t 30 -c copy output.mp4
# 快速搜尋(將 -ss 放在輸入前)
ffmpeg -ss 00:01:00 -i input.mp4 -t 30 -c copy output.mp4
合併影片
# 建立檔案列表
echo "file 'video1.mp4'" > list.txt
echo "file 'video2.mp4'" >> list.txt
echo "file 'video3.mp4'" >> list.txt
# 合併
ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4
加入浮水印
# 圖片浮水印
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=10:10" output.mp4
# 右下角
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=W-w-10:H-h-10" output.mp4
# 文字浮水印
ffmpeg -i input.mp4 -vf "drawtext=text='Copyright':fontsize=24:fontcolor=white:x=10:y=10" output.mp4
速度調整
# 加速 2 倍
ffmpeg -i input.mp4 -filter:v "setpts=0.5*PTS" -filter:a "atempo=2.0" output.mp4
# 減速 0.5 倍
ffmpeg -i input.mp4 -filter:v "setpts=2*PTS" -filter:a "atempo=0.5" output.mp4
3. 音訊處理
萃取音訊
# 萃取音訊軌道
ffmpeg -i video.mp4 -vn -c:a copy audio.aac
# 轉換為 MP3
ffmpeg -i video.mp4 -vn -c:a libmp3lame -q:a 2 audio.mp3
# 萃取特定音訊串流
ffmpeg -i video.mp4 -map 0:a:0 -c copy audio.m4a
音訊轉換
# WAV 轉 MP3
ffmpeg -i input.wav -c:a libmp3lame -b:a 320k output.mp3
# FLAC 轉 MP3
ffmpeg -i input.flac -c:a libmp3lame -q:a 0 output.mp3
# MP3 轉 AAC
ffmpeg -i input.mp3 -c:a aac -b:a 256k output.m4a
音訊標準化
# Loudnorm 濾波器(EBU R128)
ffmpeg -i input.mp3 -af loudnorm=I=-16:LRA=11:TP=-1.5 output.mp3
# 音量調整
ffmpeg -i input.mp3 -af "volume=2.0" output.mp3 # 2 倍大聲
ffmpeg -i input.mp3 -af "volume=0.5" output.mp3 # 一半音量
# 峰值標準化
ffmpeg -i input.mp3 -af "acompressor" output.mp3
合併音訊/影片
# 取代音軌
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -map 0:v -map 1:a output.mp4
# 為影片加入音訊(混合)
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -filter_complex "[0:a][1:a]amix=inputs=2:duration=first" output.mp4
4. 縮圖與螢幕截圖
單張縮圖
# 指定時間點
ffmpeg -i video.mp4 -ss 00:00:05 -frames:v 1 thumbnail.jpg
# 最佳品質
ffmpeg -i video.mp4 -ss 00:00:05 -frames:v 1 -q:v 2 thumbnail.jpg
# 指定尺寸
ffmpeg -i video.mp4 -ss 00:00:05 -frames:v 1 -vf "scale=320:180" thumbnail.jpg
多張縮圖
# 每隔 N 秒
ffmpeg -i video.mp4 -vf "fps=1/10" thumbnails_%03d.jpg # 每隔 10 秒
# 指定縮圖數量
ffmpeg -i video.mp4 -vf "select='not(mod(n,300))'" -vsync vfr thumb_%03d.jpg
# 縮圖拼貼/網格
ffmpeg -i video.mp4 -vf "fps=1/10,scale=160:90,tile=10x10" sprite.jpg
建立 GIF
# 簡單 GIF
ffmpeg -i video.mp4 -ss 0 -t 5 -vf "fps=10,scale=320:-1" output.gif
# 使用調色板的高品質 GIF
ffmpeg -i video.mp4 -ss 0 -t 5 -vf "fps=10,scale=320:-1:flags=lanczos,palettegen" palette.png
ffmpeg -i video.mp4 -i palette.png -ss 0 -t 5 -filter_complex "[0:v]fps=10,scale=320:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif
5. 串流格式
HLS(HTTP 即時串流)
# 產生 HLS
ffmpeg -i input.mp4 \
-c:v libx264 -c:a aac \
-hls_time 10 \
-hls_list_size 0 \
-hls_segment_filename "segment_%03d.ts" \
output.m3u8
# 自適應位元速率 HLS
ffmpeg -i input.mp4 \
-filter_complex "[0:v]split=3[v1][v2][v3];[v1]scale=1920:1080[v1out];[v2]scale=1280:720[v2out];[v3]scale=854:480[v3out]" \
-map "[v1out]" -c:v:0 libx264 -b:v:0 5M \
-map "[v2out]" -c:v:1 libx264 -b:v:1 2M \
-map "[v3out]" -c:v:2 libx264 -b:v:2 1M \
-map 0:a -c:a aac -b:a 128k \
-var_stream_map "v:0,a:0 v:1,a:0 v:2,a:0" \
-master_pl_name master.m3u8 \
-hls_time 6 \
-hls_segment_filename "v%v/segment_%03d.ts" \
v%v/index.m3u8
DASH(動態自適應串流)
ffmpeg -i input.mp4 \
-c:v libx264 -c:a aac \
-f dash \
-init_seg_name "init-\$RepresentationID\$.m4s" \
-media_seg_name "chunk-\$RepresentationID\$-\$Number%05d\$.m4s" \
output.mpd
6. Python 整合
PyAV(FFmpeg 綁定)
import av
def transcode_video(input_path, output_path, target_codec='libx264'):
input_container = av.open(input_path)
output_container = av.open(output_path, 'w')
# 取得輸入串流
video_stream = input_container.streams.video[0]
audio_stream = input_container.streams.audio[0] if input_container.streams.audio else None
# 建立輸出串流
output_video = output_container.add_stream(target_codec, rate=video_stream.average_rate)
output_video.width = video_stream.width
output_video.height = video_stream.height
output_video.pix_fmt = 'yuv420p'
if audio_stream:
output_audio = output_container.add_stream('aac', rate=audio_stream.rate)
for packet in input_container.demux():
if packet.stream.type == 'video':
for frame in packet.decode():
for out_packet in output_video.encode(frame):
output_container.mux(out_packet)
elif packet.stream.type == 'audio' and audio_stream:
for frame in packet.decode():
for out_packet in output_audio.encode(frame):
output_container.mux(out_packet)
# 刷新編碼器
for packet in output_video.encode():
output_container.mux(packet)
output_container.close()
input_container.close()
MoviePy
from moviepy.editor import VideoFileClip, concatenate_videoclips, TextClip, CompositeVideoClip
def process_video(input_path, output_path):
# 載入影片
clip = VideoFileClip(input_path)
# 修剪
trimmed = clip.subclip(10, 60) # 10 秒到 60 秒
# 調整尺寸
resized = trimmed.resize(height=720)
# 加入文字疊加
txt_clip = TextClip("Hello World", fontsize=50, color='white')
txt_clip = txt_clip.set_position('center').set_duration(5)
# 合成
final = CompositeVideoClip([resized, txt_clip])
# 匯出
final.write_videofile(
output_path,
codec='libx264',
audio_codec='aac'
)
clip.close()
def create_thumbnail(video_path, output_path, time=5):
clip = VideoFileClip(video_path)
frame = clip.get_frame(time)
clip.save_frame(output_path, t=time)
clip.close()
def concatenate_videos(video_paths, output_path):
clips = [VideoFileClip(path) for path in video_paths]
final = concatenate_videoclips(clips)
final.write_videofile(output_path)
for clip in clips:
clip.close()
FFmpeg 子程序
import subprocess
import json
def get_video_info(video_path):
"""使用 ffprobe 取得影片中繼資料"""
cmd = [
'ffprobe',
'-v', 'quiet',
'-print_format', 'json',
'-show_format',
'-show_streams',
video_path
]
result = subprocess.run(cmd, capture_output=True, text=True)
return json.loads(result.stdout)
def transcode_video(input_path, output_path, options=None):
"""使用 FFmpeg 轉碼影片"""
cmd = ['ffmpeg', '-i', input_path]
if options:
if 'video_codec' in options:
cmd.extend(['-c:v', options['video_codec']])
if 'audio_codec' in options:
cmd.extend(['-c:a', options['audio_codec']])
if 'crf' in options:
cmd.extend(['-crf', str(options['crf'])])
if 'resolution' in options:
cmd.extend(['-vf', f"scale={options['resolution']}"])
cmd.extend(['-y', output_path])
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise Exception(f"FFmpeg 錯誤:{result.stderr}")
return True
def generate_hls(input_path, output_dir):
"""產生 HLS 串流"""
import os
os.makedirs(output_dir, exist_ok=True)
cmd = [
'ffmpeg', '-i', input_path,
'-c:v', 'libx264',
'-c:a', 'aac',
'-hls_time', '10',
'-hls_list_size', '0',
'-hls_segment_filename', f'{output_dir}/segment_%03d.ts',
f'{output_dir}/index.m3u8'
]
subprocess.run(cmd, check=True)
7. 直播
RTMP 伺服器(nginx-rtmp)
# nginx.conf
rtmp {
server {
listen 1935;
application live {
live on;
record off;
# HLS 輸出
hls on;
hls_path /var/www/hls;
hls_fragment 3;
hls_playlist_length 60;
}
}
}
串流至 RTMP
# 將檔案串流至 RTMP
ffmpeg -re -i input.mp4 -c copy -f flv rtmp://server/live/stream
# 串流網路攝影機
ffmpeg -f v4l2 -i /dev/video0 -f alsa -i hw:0 \
-c:v libx264 -preset veryfast -b:v 2M \
-c:a aac -b:a 128k \
-f flv rtmp://server/live/stream
# 串流至 YouTube
ffmpeg -i input.mp4 \
-c:v libx264 -preset medium -b:v 4M \
-c:a aac -b:a 128k \
-f flv rtmp://a.rtmp.youtube.com/live2/YOUR_STREAM_KEY
8. 影片處理管線
使用 Celery 進行非同步處理
from celery import Celery
import os
celery = Celery('video_tasks')
@celery.task
def process_upload(video_id, input_path):
"""完整的影片處理管線"""
output_dir = f'/videos/{video_id}'
os.makedirs(output_dir, exist_ok=True)
# 產生縮圖
generate_thumbnail.delay(video_id, input_path, f'{output_dir}/thumb.jpg')
# 轉碼為多種解析度
resolutions = [
('1080p', '1920:1080', '5M'),
('720p', '1280:720', '2.5M'),
('480p', '854:480', '1M'),
]
for name, scale, bitrate in resolutions:
transcode_resolution.delay(
video_id, input_path,
f'{output_dir}/{name}.mp4',
scale, bitrate
)
# 產生 HLS
generate_hls.delay(video_id, input_path, f'{output_dir}/hls')
@celery.task
def generate_thumbnail(video_id, input_path, output_path):
cmd = [
'ffmpeg', '-i', input_path,
'-ss', '5', '-frames:v', '1',
'-vf', 'scale=320:-1',
output_path
]
subprocess.run(cmd, check=True)
@celery.task
def transcode_resolution(video_id, input_path, output_path, scale, bitrate):
cmd = [
'ffmpeg', '-i', input_path,
'-c:v', 'libx264', '-b:v', bitrate,
'-vf', f'scale={scale}',
'-c:a', 'aac', '-b:a', '128k',
output_path
]
subprocess.run(cmd, check=True)
9. 音訊分析
波形生成
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
def generate_waveform(audio_path, output_path):
"""產生波形圖片"""
y, sr = librosa.load(audio_path)
plt.figure(figsize=(14, 5))
librosa.display.waveshow(y, sr=sr)
plt.title('波形')
plt.savefig(output_path, dpi=150, bbox_inches='tight')
plt.close()
def generate_spectrogram(audio_path, output_path):
"""產生頻譜圖圖片"""
y, sr = librosa.load(audio_path)
D = librosa.stft(y)
S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
plt.figure(figsize=(14, 5))
librosa.display.specshow(S_db, sr=sr, x_axis='time', y_axis='hz')
plt.colorbar(format='%+2.0f dB')
plt.title('頻譜圖')
plt.savefig(output_path, dpi=150, bbox_inches='tight')
plt.close()
10. 最佳實踐
編碼預設
# 網頁最佳化 MP4
ffmpeg -i input.mp4 \
-c:v libx264 -preset slow -crf 22 \
-c:a aac -b:a 128k \
-movflags +faststart \
output.mp4
# 歸檔品質
ffmpeg -i input.mp4 \
-c:v libx264 -preset veryslow -crf 18 \
-c:a flac \
archive.mkv
# 行動裝置最佳化
ffmpeg -i input.mp4 \
-c:v libx264 -preset fast -crf 28 \
-vf "scale='min(720,iw)':-2" \
-c:a aac -b:a 96k \
mobile.mp4
最佳實踐
- 使用硬體加速 - NVENC、VAAPI、VideoToolbox
- 為網頁最佳化 - faststart 旗標
...
[為翻譯而截斷]