当前位置:网站首页>Ffmpeg usage in video compression processing
Ffmpeg usage in video compression processing
2022-06-28 19:15:00 【No regrets!】
List of articles
0、 Through the video compression function , Compress the video frame rate
First of all, we should understand that video is composed of pictures , For example, we can tell by the naked eye every second 24 About a picture , That is, when it plays us every second 24 More than pictures , We can't tell if it's a picture , It feels like a continuous action , This is the video , And normal videos, whether we record them ourselves , It is also a common video format , Frame rates are higher than 25 Frame above ,
Therefore, the content of this section is to count the video frames , Down to 10 Frames per second , This reduces the size of the video , Video compression
Of course, there are many ways of video compression , This is only the more common and easy to implement method .
Can pass python command , Change to python Script , All changes are inseparable from the root
os.system("ffmpeg XXXX")
perhaps
import subprocess
(status, output) = subprocess.getstatusoutput("ffmpeg XXXX")
In the end python The code batch processes the video
One 、ffmpeg install
Because of the present win10 The system can be installed Ubuntu Subsystem , Therefore, most of my software has been installed in Ubuntu In the subsystem , This is convenient for installation, operation and management , This article is about ffmpeg The examples of the video processing toolkit are all by Ubuntu complete .
Installation command sudo apt install ffmpeg
Two 、 Compressed video
[email protected]:12_ Video processing $ ffmpeg -i "2.mp4" -r 10 -b:a 32k save2.mp4
among
| command | meaning |
|---|---|
-i "2.mp4" | Enter the video name as 2.mp4, Note the quotation marks |
-r 10 | Set up 10 frame /s |
-b:a 32k | Set the audio bit rate to 32kb/s namely 4kB/s |
save2.mp4 | Set the output file name |
effect 
[email protected]:12_ Video processing $ ffmpeg -i "2.mp4" -r 10 -b:a 32k save2.mp4
ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '2.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp41isom
creation_time : 2020-03-10T13:12:18.000000Z
title : Neural vectorization : End to end depth vectorization reinforcement learning - OneNote
artist : Microsoft Game DVR
Duration: 00:00:06.51, start: 0.000000, bitrate: 8795 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 8675 kb/s, 29.69 fps, 60 tbr, 30k tbn, 59.94 tbc (default)
Metadata:
creation_time : 2020-03-10T13:12:18.000000Z
handler_name : VideoHandler
encoder : AVC Coding
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 120 kb/s (default)
Metadata:
creation_time : 2020-03-10T13:12:18.000000Z
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x7fffdff91780] using SAR=1/1
[libx264 @ 0x7fffdff91780] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x7fffdff91780] profile High, level 4.0
[libx264 @ 0x7fffdff91780] 264 - core 152 r2854 e9a5903 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=10 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'save2.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp41isom
artist : Microsoft Game DVR
title : Neural vectorization : End to end depth vectorization reinforcement learning - OneNote
encoder : Lavf57.83.100
Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 10 fps, 10240 tbn, 10 tbc (default)
Metadata:
creation_time : 2020-03-10T13:12:18.000000Z
handler_name : VideoHandler
encoder : Lavc57.107.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 32 kb/s (default)
Metadata:
creation_time : 2020-03-10T13:12:18.000000Z
handler_name : SoundHandler
encoder : Lavc57.107.100 aac
frame= 67 fps= 48 q=-1.0 Lsize= 455kB time=00:00:06.50 bitrate= 573.0kbits/s dup=0 drop=126 speed=4.62x
video:425kB audio:26kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.987973%
[libx264 @ 0x7fffdff91780] frame I:1 Avg QP:14.29 size:327526
[libx264 @ 0x7fffdff91780] frame P:20 Avg QP:19.98 size: 4573
[libx264 @ 0x7fffdff91780] frame B:46 Avg QP:23.36 size: 336
[libx264 @ 0x7fffdff91780] consecutive B-frames: 3.0% 14.9% 4.5% 77.6%
[libx264 @ 0x7fffdff91780] mb I I16..4: 50.0% 9.9% 40.1%
[libx264 @ 0x7fffdff91780] mb P I16..4: 0.9% 0.5% 0.0% P16..4: 8.3% 0.2% 0.2% 0.0% 0.0% skip:89.8%
[libx264 @ 0x7fffdff91780] mb B I16..4: 0.1% 0.1% 0.0% B16..8: 3.8% 0.0% 0.0% direct: 0.1% skip:95.8% L0:18.6% L1:81.4% BI: 0.0%
[libx264 @ 0x7fffdff91780] 8x8 transform intra:17.3% inter:50.2%
[libx264 @ 0x7fffdff91780] coded y,uvDC,uvAC intra: 22.2% 32.8% 26.3% inter: 0.6% 2.0% 0.9%
[libx264 @ 0x7fffdff91780] i16 v,h,dc,p: 80% 20% 0% 0%
[libx264 @ 0x7fffdff91780] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 4% 69% 0% 0% 0% 0% 0% 0%
[libx264 @ 0x7fffdff91780] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 35% 31% 12% 3% 3% 3% 4% 4% 5%
[libx264 @ 0x7fffdff91780] i8c dc,h,v,p: 67% 13% 18% 1%
[libx264 @ 0x7fffdff91780] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x7fffdff91780] ref P L0: 82.5% 1.5% 14.5% 1.6%
[libx264 @ 0x7fffdff91780] ref B L0: 75.7% 23.1% 1.2%
[libx264 @ 0x7fffdff91780] ref B L1: 97.0% 3.0%
[libx264 @ 0x7fffdff91780] kb/s:518.72
[aac @ 0x7fffdff9b9c0] Qavg: 574.607
[email protected]:12_ Video processing $
3、 ... and 、 Common commands
0. Remove sounds for a specified period of time , here between t Time 0-237s All sounds between are deleted
ffmpeg -i temperature.mp4 -af "volume=enable='between(t,0,237)':volume=0" out.mp4
1. Separate video and audio streams
ffmpeg -i input_file -vcodec copy -an output_file_video // Separate video streams
ffmpeg -i input_file -acodec copy -vn output_file_audio // Separate audio streams
2. Video demultiplexing
ffmpeg –i test.mp4 –vcodec copy –an –f m4v test.264
ffmpeg –i test.avi –vcodec copy –an –f m4v test.264
3. Video transcoding
ffmpeg –i test.mp4 –vcodec h264 –s 352*278 –an –f m4v test.264 // Transcoding is the original file of code stream
ffmpeg –i test.mp4 –vcodec h264 –bf 0 –g 25 –s 352*278 –an –f m4v test.264 // Transcoding is the original file of code stream
ffmpeg –i test.avi -vcodec mpeg4 –vtag xvid –qsame test_xvid.avi // Transcoding to encapsulation file
//-bf B Frame number control ,-g Keyframe interval control ,-s Resolution control
4. Video packaging
ffmpeg –i video_file –i audio_file –vcodec copy –acodec copy output_file
5. Video clip
ffmpeg –i test.avi –r 1 –f image2 image-%3d.jpeg // To extract the image
ffmpeg -ss 0:1:30 -t 0:0:20 -i input.avi -vcodec copy -acodec copy output.avi // Cut video
//-r Extract the frequency of the image ,-ss Starting time ,-t The duration of the
6. video recording
ffmpeg –i rtsp://192.168.3.205:5555/test –vcodec copy out.avi
7.YUV Sequence play
ffplay -f rawvideo -video_size 1920x1080 input.yuv
8.YUV The sequence turns AVI
ffmpeg –s w*h –pix_fmt yuv420p –i input.yuv –vcodec mpeg4 output.avi
9. Get the cover
ffmpeg -ss 00:00:10 -i test1.flv -f image2 -y test1.jpg
Description of common parameters :
main parameter :
-i Set input flow
-f Set output format
-ss Starting time
Video parameters :
-b Set video traffic , The default is 200Kbit/s
-r Set the frame rate , The default is 25
-s Set the width and height of the screen
-aspect Set the scale of the screen
-vn Don't process video
-vcodec Set the video codec , If it is not set, the same codec as the input stream will be used
Audio parameters :
-ar Set the sampling rate
-ac Set the sound Channel Count
-acodec Set up the sound codec , If it is not set, the same codec as the input stream will be used
-an Don't handle audio
-L license
-h help
-fromats Show available formats , Codec , Agreed ...
-f fmt Force format fmt
-I filename Input file
-y Overwrite output file
-t duration Set the recording time hh:mm:ss[.xxx] The format of recording time also supports
-ss position Search to the specified time [-]hh:mm:ss[.xxx] The format also supports
-title string Set title
-author string Set the author
-copyright string Set copyright
-comment string Set up comments
-target type Set the target file type (vcd,svcd,dvd) All the format options ( Bit rate , Codec and buffer size ) Automatic setting , Just type in the following :
ffmpeg -i myfile.avi -target vcd /tmp/vcd.mpg
-hq Activate high quality settings
-itsoffset offset Set the time offset in seconds , This option affects all subsequent input files . The offset is added to the timestamp of the input file , Defining a positive offset means that the corresponding stream is delayed offset second . [-]hh:mm:ss[.xxx] The format also supports
Four 、 Code

# The function of the script is , All videos in the folder to be compressed , adopt ffmpeg
# Frame rate compression , Save the compressed file to the processed folder
# The execution order is In the installation ffmpeg In the system , python passion.py
import os
import sys
wait_handle_dir = r" To be compressed /"
save_dir = r" processed /"
if os.path.exists(save_dir):
pass
else:
os.mkdir(save_dir)
for file in os.listdir(wait_handle_dir):
save_name = f"{os.path.splitext(file)[0]}.mp4"
if save_name not in os.listdir(save_dir): # Judge whether there is converted data in the folder
print(f"trackle {wait_handle_dir}{file} ..................................")
os.system(f'ffmpeg -i "{wait_handle_dir}{file}" -r 10 -b:a 32k "{save_dir}{save_name}"')
update 2020/9/17 Add parallel processing

# passion.py
# The function of the script is , All videos in the folder to be compressed , adopt ffmpeg
# Frame rate compression , Save the compressed file to the processed folder
# The execution order is In the installation ffmpeg In the system , python passion.py Folder of video to be compressed
import os
import sys
import ray # If there is an error here , Please install ray package pip install ray
import time
time_now = time.time()
wait_handle_dir = str(sys.argv[1])
if "/" not in wait_handle_dir:
wait_handle_dir = wait_handle_dir+"/"
save_dir = wait_handle_dir[:-1] + "_handle/"
print(f" The results will be saved to the path {save_dir} Inside ")
if os.path.exists(save_dir):
pass
else:
os.mkdir(save_dir)
ray.init()
@ray.remote
def handle_single(file):
save_name = f"{os.path.splitext(file)[0]}.mp4"
if save_name not in os.listdir(save_dir): # Judge whether there is converted data in the folder
print(f"trackle {wait_handle_dir}{file} ..................................")
os.system(f'ffmpeg -i "{wait_handle_dir}{file}" -r 10 -b:a 32k "{save_dir}{save_name}"')
ray.get([handle_single.remote(file) for file in os.listdir(wait_handle_dir)])
# Process all files in parallel , The speed depends on the computer CPU Number of cores , Such as four cores and eight threads Then eight files will be processed at one time, no more words
print(" when :", time.time()-time_now)
Here are some links that summarize common commands
ffmpeg Command parameter details
Video compression tools ffmpeg Use
python Video compression
边栏推荐
- 基于管线的混合渲染
- mysql全解 Ubuntu/win10
- Upward and downward transformation
- Michael Wooldridge, professeur à l'Université d'Oxford: comment la communauté de l'IA voit les réseaux neuronaux depuis près de 40 ans
- Leetcode 周赛299
- Modular operation
- Differences and relations among rxjs map, mergemap and switchmap
- Cross cluster deployment of helm applications using karmada
- Win11如何给系统盘瘦身?Win11系统盘瘦身方法
- 声网发布灵隼物联网云平台 可一小时构建示例场景
猜你喜欢

智能计算系统1 环境搭建

3D可旋转粒子矩阵

OpenHarmony—内核对象事件之源码详解

道路千万条,为什么这家创新存储公司会选这条?

i人事HR系统上架企业微信ISV,增强企微在服务连锁零售等行业深度应用

数据基础设施升级窗口下,AI 新引擎的技术方法论

C# 41. int与string互转

春风动力携手华为打造智慧园区标杆,未来工厂创新迈上新台阶

How to resolve kernel errors? Solution to kernel error of win11 system

How to change the status bar at the bottom of win11 to black? How to change the status bar at the bottom of win11 to black
随机推荐
易观分析《2022年中国银行业隐私计算平台供应商实力矩阵分析》研究活动 正式启动
Idea merge other branches into dev branch
pd. Difference between before and after cut interval parameter setting
[C #] explain the difference between value type and reference type
CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识
Anonymous function this pointing and variable promotion
leetcode 1647. Minimum deletions to make character frequencies unique
电脑如何检查驱动程序是否正常
3D可旋转粒子矩阵
牛津大學教授Michael Wooldridge:AI社區近40年如何看待神經網絡
PCL 计算平面三角形外接圆的圆心和半径
毕业设计-基于Unity的餐厅经营游戏的设计与开发(附源码、开题报告、论文、答辩PPT、演示视频,带数据库)
大火的虚拟人在哪些产业开始发力?
道路千万条,为什么这家创新存储公司会选这条?
Web3 that unleashes the value of the Internet
Month on month SQL implementation
月环比sql实现
泰山OFFICE技术讲座:WORD奇怪的字体高度
Render function parsing
grafana绘制走势图