汇总 ffmpeg 实用案例

ffmpeg 转码流程是什么

FFmpeg 是一个开源的多媒体处理工具,它可以进行音视频的编解码、转码、处理等操作。下面是使用 FFmpeg 进行转码的一般流程:

  1. 安装 FFmpeg:首先需要安装 FFmpeg 工具,你可以从官方网站或使用包管理器(如apt、yum、brew等)进行安装。

  2. 准备输入文件:需要转码的音视频文件作为输入文件。

  3. 编写转码命令:使用 FFmpeg 命令行工具编写转码命令,指定输入文件、输出文件、转码参数等。

  4. 执行转码命令:在命令行中执行编写的转码命令,FFmpeg 将根据指定的参数和配置进行转码操作。

以下是一个简单的示例,展示了使用 FFmpeg 将一个视频文件转码为另一种格式的基本流程:

ffmpeg -i input.mp4 -c:v libx264 -c:a aac -strict -2 output.mp4

解释:

  • -i input.mp4:指定输入文件为 input.mp4
  • -c:v libx264:选择视频编码器为 libx264(H.264编码器)。
  • -c:a aac:选择音频编码器为 AAC。
  • -strict -2:指定使用 AAC 编码器时的音频规范,-2 表示允许使用实验性的 AAC 编码器。
  • output.mp4:指定输出文件为 output.mp4

在执行上述命令后,FFmpeg 将会对 input.mp4 进行转码,并将结果保存为 output.mp4

需要注意的是,FFmpeg 支持非常多的编解码器、滤镜和参数选项,可以根据具体的需求来配置转码命令,如调整视频分辨率、码率、帧率、音频采样率等参数。因此,转码流程的具体细节和参数设置会根据实际需求而有所不同。

常见的视频格式包含哪些内容

常见的视频格式通常包括容器格式、视频编码格式和音频编码格式。以下是几种常见的视频格式及其内容:

  1. MP4(MPEG-4 Part 14):

    • 容器格式:MP4
    • 视频编码格式:H.264(AVC),H.265(HEVC),MPEG-4 Part 2
    • 音频编码格式:AAC,MP3
  2. AVI(Audio Video Interleave):

    • 容器格式:AVI
    • 视频编码格式:DivX,Xvid,MPEG-4 Part 2
    • 音频编码格式:MP3,AC3
  3. MKV(Matroska Video):

    • 容器格式:MKV
    • 视频编码格式:H.264(AVC),H.265(HEVC),VP9
    • 音频编码格式:AAC,MP3,AC3,DTS
  4. MOV(QuickTime):

    • 容器格式:MOV
    • 视频编码格式:H.264(AVC),ProRes
    • 音频编码格式:AAC,PCM
  5. WMV(Windows Media Video):

    • 容器格式:ASF,WMV
    • 视频编码格式:WMV,VC-1
    • 音频编码格式:WMA
  6. FLV(Flash Video):

    • 容器格式:FLV
    • 视频编码格式:H.264(AVC),VP6,VP8
    • 音频编码格式:AAC,MP3
  7. WebM

    • 容器格式:WebM
    • 视频编码格式:VP8,VP9
    • 音频编码格式:Opus,Vorbis
  8. OGG(Ogg Media):

    • 容器格式:OGG
    • 视频编码格式:Theora
    • 音频编码格式:Vorbis

这些视频格式中,容器格式用于存储视频、音频和元数据,并且可以支持多种视频编码和音频编码格式。不同的视频格式可能具有不同的特性和优劣势,因此在选择使用哪种视频格式时,需要根据实际需求考虑各种因素,如兼容性、视频质量、文件大小等。

对视频文件进行抽取内容?

from moviepy.editor import VideoFileClip

# 加载视频文件
video = VideoFileClip("input.mp4")

# 提取视频和音频
video_clip = video.subclip()
audio_clip = video.audio

# 保存视频和音频
video_clip.write_videofile("output_video.mp4")
audio_clip.write_audiofile("output_audio.mp3")

用tkinter写一个视频转码:

# -*- coding: utf-8 -*-

from ttkbootstrap import Style
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
import subprocess

# 创建主应用程序窗口
root = tk.Tk()

# 创建一个 ttkbootstrap 样式对象
style = Style(theme='flatly')

# 将样式应用于根窗口
style.theme_use()

# 添加一个标签
label = ttk.Label(root, text="选择要转码的视频文件:", style='primary.TLabel')
label.pack()

# 添加一个按钮,用于选择视频文件
def choose_video_file():
    filepath = filedialog.askopenfilename()
    if filepath:
        input_entry.delete(0, tk.END)
        input_entry.insert(0, filepath)

input_entry = ttk.Entry(root, width=40, style='info.TEntry')
input_entry.pack()

choose_button = ttk.Button(root, text="选择文件", style='success.TButton', command=choose_video_file)
choose_button.pack()

# 添加一个下拉菜单,用于选择输出视频格式
formats = ['.mp4', '.avi', '.mov']
selected_format = tk.StringVar(value=formats[0])
format_label = ttk.Label(root, text="选择输出视频格式:", style='primary.TLabel')
format_label.pack()

format_dropdown = ttk.Combobox(root, values=formats, textvariable=selected_format, style='info.TCombobox')
format_dropdown.pack()

# 添加一个按钮,用于执行转码操作
def transcode_video():
    input_file = input_entry.get()
    output_format = selected_format.get()[1:]  # Remove the leading dot from the format

    if input_file:
        output_file = input_file.rsplit('.', 1)[0] + '.' + output_format
        command = f"ffmpeg -i {input_file} {output_file}"

        try:
            subprocess.run(command, shell=True, check=False)  # 不检查返回值
            result_label.config(text=f"转码完成!\n输出文件路径:{output_file}")  # 更新结果标签
        except subprocess.CalledProcessError as e:
            result_label.config(text=f"转码失败:{e}")

transcode_button = ttk.Button(root, text="开始转码", style='danger.TButton', command=transcode_video)
transcode_button.pack()

# 添加一个结果标签
result_label = ttk.Label(root, text="", style='warning.TLabel')
result_label.pack()

# 运行主事件循环
root.mainloop()

这里哪里用到了 ffmpeg?

output_file = input_file.rsplit('.', 1)[0] + '.' + output_format
        command = f"ffmpeg -i {input_file} {output_file}"

用moviepy 对视频文件进行放大缩小:

from moviepy.editor import VideoFileClip

# 加载视频文件
video = VideoFileClip("input.mp4")

# 放大视频(缩小同理,调整scale值)
scaled_video = video.resize((video.w * 2, video.h * 2))

# 保存放大后的视频
scaled_video.write_videofile("output_scaled.mp4")

用ffmpeg来写放大缩小视频:

import ffmpeg

# 定义输入文件、输出文件和缩放比例
input_file = "input.mp4"
output_file_scaled = "output_scaled.mp4"
output_file_resized = "output_resized.mp4"

# 放大视频
(
    ffmpeg
    .input(input_file)
    .output(output_file_scaled, vf='scale=iw*2:ih*2')
    .run()
)

# 缩小视频
(
    ffmpeg
    .input(input_file)
    .output(output_file_resized, vf='scale=iw/2:ih/2')
    .run()
)

ffmpeg是视频加logo:

ffmpeg -i input_video.mp4 -i logo.png -filter_complex "overlay=10:10" output_video_with_logo.mp4

ffmpeg是视频加文本:

ffmpeg -i input_video.mp4 -vf "drawtext=text='Hello, World!':x=10:y=10:fontsize=24:fontcolor=white" output_video_with_text.mp4

ffmpeg 录制视频:

从摄像头录制视频:

ffmpeg -f v4l2 -i /dev/video0 output.mp4

从屏幕录制视频:

ffmpeg -f x11grab -r 25 -s 1920x1080 -i :0.0 -c:v libx264 -preset ultrafast output.mp4

ffmpeg 截图:

ffmpeg -i input_video.mp4 -ss 00:00:05 -vframes 1 output_image.jpg

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值