ffmpeg 视频压缩
时间: 2023-10-17 08:06:31 浏览: 61
FFmpeg是一个强大的开源多媒体框架,可以在命令行中使用它来修改和压缩视频文件格式。下面是使用FFmpeg进行视频压缩的命令示例:
ffmpeg -y -i input_video.avi -s 768x576 -vcodec libx264 -preset fast -b 10000 -r 10 output_video.mp4
在这个命令中,你需要将"input_video.avi"替换为你要压缩的视频文件的路径和文件名,"output_video.mp4"替换为你希望生成的压缩后的视频的路径和文件名。下面是命令中使用的参数的解释:
- "-y"参数表示在输出文件已存在的情况下自动覆盖。
- "-i"参数后跟着输入视频文件的路径和文件名。
- "-s"参数后跟着输出视频的分辨率,这里示例中设置为768x576。
- "-vcodec"参数后跟着视频编解码器的名称,这里示例中使用libx264作为视频编解码器。
- "-preset"参数后跟着视频编码速度和压缩比的设置,这里示例中设置为fast以获得较快的压缩速度。
- "-b"参数后跟着视频的比特率,这里示例中设置为10000 kbps。
- "-r"参数后跟着输出视频的帧率,这里示例中设置为10fps。
请注意,以上命令只是一个示例,你可以根据自己的需求调整参数来实现不同的视频压缩效果。另外,你也可以通过编程语言如C#来使用FFmpeg进行视频压缩,只需要调用FFmpeg的相关函数即可。
相关问题
ffmpeg视频压缩代码
FFmpeg是一个开源的跨平台音视频处理框架,它提供了非常强大的音视频处理功能。其中,视频压缩是FFmpeg中的一个非常重要的功能之一。下面是一个使用FFmpeg进行视频压缩的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
int main(int argc, char *argv[]) {
AVFormatContext *pFormatCtx = NULL;
int i, videoStream;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVPacket packet;
int frameFinished;
struct SwsContext *sws_ctx = NULL;
if (argc < 3) {
printf("Usage: %s <input_file> <output_file>\n", argv);
return -1;
}
av_register_all();
if (avformat_open_input(&pFormatCtx, argv, NULL, NULL) != 0) {
return -1;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
return -1;
}
av_dump_format(pFormatCtx, 0, argv, 0);
videoStream = -1;
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break;
}
}
if (videoStream == -1) {
return -1;
}
pCodecCtx = avcodec_alloc_context3(NULL);
if (!pCodecCtx) {
return -1;
}
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar);
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
return -1;
}
pFrame = av_frame_alloc();
sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P,
SWS_BILINEAR, NULL, NULL, NULL);
FILE *fp_out = fopen(argv, "wb");
while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if (frameFinished) {
uint8_t *out_buffer = (uint8_t *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));
av_image_fill_arrays(pFrame->data, pFrame->linesize, out_buffer,
AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
sws_scale(sws_ctx, (const uint8_t* const*)pFrame->data,
pFrame->linesize, 0, pCodecCtx->height,
pFrame->data, pFrame->linesize);
AVPacket pkt = { 0 };
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
int got_output = 0;
AVCodecContext *enc_ctx = avcodec_alloc_context3(NULL);
if (!enc_ctx) {
return -1;
}
enc_ctx->width = pCodecCtx->width / 2;
enc_ctx->height = pCodecCtx->height / 2;
enc_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
enc_ctx->bit_rate = pCodecCtx->bit_rate / 4;
enc_ctx->time_base = (AVRational){ 1, 25 };
enc_ctx->gop_size = 10;
enc_ctx->max_b_frames = 1;
enc_ctx->thread_count = 4;
enc_ctx->qmin = 10;
enc_ctx->qmax = 51;
enc_ctx->qcompress = 0.6f;
AVCodec *enc_codec = avcodec_find_encoder_by_name("libx264");
if (!enc_codec) {
return -1;
}
if (avcodec_open2(enc_ctx, enc_codec, NULL) < 0) {
return -1;
}
if (avcodec_encode_video2(enc_ctx, &pkt, pFrame, &got_output) >= 0 && got_output) {
fwrite(pkt.data, pkt.size, 1, fp_out);
av_packet_unref(&pkt);
}
av_free(out_buffer);
avcodec_free_context(&enc_ctx);
}
}
av_packet_unref(&packet);
}
fclose(fp_out);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}
```
这段代码实现了从输入文件中读取视频帧,将其压缩后写入到输出文件中。其中使用了libx264作为视频编码器,压缩后的视频格式为H.264。你可以根据自己的需求修改编码器的参数来得到不同的压缩效果。
vue ffmpeg视频压缩代码
Vue.js是一种流行的JavaScript框架,用于构建用户界面。而FFmpeg是一个强大的开源多媒体处理工具,可以用于处理音频和视频文件。下面是一个简单的Vue.js代码示例,用于使用FFmpeg进行视频压缩:
```javascript
<template>
<div>
<input type="file" @change="handleFileChange" accept="video/*">
<button @click="compressVideo">压缩视频</button>
</div>
</template>
<script>
export default {
data() {
return {
videoFile: null,
};
},
methods: {
handleFileChange(event) {
this.videoFile = event.target.files;
},
compressVideo() {
if (!this.videoFile) {
alert("请先选择一个视频文件");
return;
}
const ffmpeg = require("ffmpeg.js/ffmpeg-mp4.min.js");
const worker = new Worker(ffmpeg);
worker.onmessage = (event) => {
const { data } = event;
if (data.type === "ready") {
// FFmpeg准备就绪
worker.postMessage({
type: "run",
arguments: [
"-i",
this.videoFile.name,
"-vf",
"scale=640:480",
"output.mp4",
],
});
} else if (data.type === "stdout") {
// FFmpeg输出日志
console.log(data.data);
} else if (data.type === "done") {
// 压缩完成
const outputBuffer = data.data.MEMFS.data;
const outputBlob = new Blob([outputBuffer], { type: "video/mp4" });
const outputUrl = URL.createObjectURL(outputBlob);
const link = document.createElement("a");
link.href = outputUrl;
link.download = "compressed.mp4";
link.click();
}
};
// 加载FFmpeg核心
worker.postMessage({ type: "load" });
},
},
};
</script>
```
上述代码中,我们首先使用`<input type="file">`元素让用户选择一个视频文件。然后,当用户选择文件后,我们将其保存在`videoFile`变量中。接下来,当用户点击“压缩视频”按钮时,我们使用FFmpeg进行视频压缩。
在压缩视频的过程中,我们使用了`ffmpeg.js`库,它是FFmpeg的JavaScript版本。我们创建了一个`Worker`对象,并加载了FFmpeg核心。然后,我们通过`postMessage`方法向Worker发送消息,告诉它要执行的命令。在这个例子中,我们使用了`-vf scale=640:480`参数来将视频缩放为640x480的尺寸,并将压缩后的视频保存为`output.mp4`文件。
当FFmpeg处理完成后,它会通过`onmessage`事件返回结果。我们可以通过检查返回的数据类型来确定消息的类型。当类型为`done`时,表示压缩完成,我们可以从返回的数据中获取压缩后的视频文件,并将其下载到本地。