ffmpeg下载ts缓存

ffmpeg -i https://hls/index.m3u8 -acodec copy -vcodec copy output.ts
这样就会自动将.m3u8记录的ts文件进行下载然后自动整合为一个新的output.ts

  • (1) -an: 去掉音频
  • (2) -acodec: 音频选项, 一般后面加copy表示拷贝
  • (3) -vcodec:视频选项,一般后面加copy表示拷贝
  • (4) -shortest:会自动选择音视频中最短的一个为标准,比如20s音频,10s视频,最后合成为10s

视频转换

ts 转 mp4
ffmpeg -i test.ts -acodec copy -vcodec copy -f mp4 test.mp4

将mp4文件分出mp3

ffmpeg -i 1.mp4 -f mp3 -vn apple.mp3

将mp3文件进行截取

ffmpeg -i apple.mp3 -ss 00:00:00 -t 00:04:18 -acodec copy output.mp3

添加请求头

有一些下载流文件的请求需要验证请求头,这个时候我需要在命令行中添加

1
2
# 多个请求头之间使用$'\r\n'进行分割
ffmpeg -user_agent "User-Agent: PostmanRuntime/7.28.3" -headers "Host: vod2eu38.128100.xyz"$'\r\n' -i 'https://vod2eu38.128100.xyz/hls/nCKfluKt0NL/index.m3u8?token=UHJUaE5VRmhqdHl3amw4OVBuUjZIdHN6NXI1SWlxZUMremI1K2hROHF6N0RDcDZLVGFDYzFEemliZ0IzbnNUcU4xcDYreHMzVjBuVU13UUZVUWVGTEg1L3RXbmcycG5DdGh3TkxWaVdLdW5kUnA2RTZ3WVgya1lGeXF6eHlFakQ=&ip=61.227.36.77&auth=dc784ea58ee35fbdfc140d7b85ba4a32&exp=1636360339&hash=d146c893324bb897ad0043c80189a87b' -c copy str4.mp4

多个视频合并

ffmpeg -f concat -safe 0 -i /Users/keeep/py-project/pythonSpider/movie.txt -c copy /Users/keeep/output.mp4

思路是先将需要合并的文件列出为一个txt文件,python例子如下

1
2
3
4
5
6
7
8
9
10
11
12
def file_name(file_dir):
if exists('movie.txt'):
os.remove('movie.txt')
with open('movie.txt', 'a') as movie_txt:
for root, dirs, files in os.walk(file_dir):
for file in files: # type: str
if os.path.splitext(file)[1] == '.mp4':
if file.find('\''):
replace_name = file.replace('\'', '\"')
os.rename(file_dir + os.sep + file, file_dir + os.sep + replace_name)
file = replace_name
movie_txt.write('file' + ' \'' + root + '/' + file + '\'\n')

结合Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from ffmpy3 import FFmpeg
# 音视频分开的例子
async def ffmpeg_exec(inputs_path, outputs_path):
split = re.split(',', inputs_path)
video_url = split[0]
audio_url = split[1]
a = FFmpeg(
executable='/opt/homebrew/bin/ffmpeg',
inputs={video_url: None, audio_url: None},
outputs={outputs_path: '-c copy'}
)
await a.run_async()
await a.wait()
# m3u8例子
async def ffmpeg_exec(inputs_path, outputs_path):
a = FFmpeg(
executable='/opt/homebrew/bin/ffmpeg',
inputs={inputs_path: None},
outputs={outputs_path: '-c copy'}
)
await a.run_async()
await a.wait()
# 设置header的非异步例子
def ffmpeg_exec_single(inputs_path, outputs_path):
result = re.match('https://(.*?)/.*', inputs_path)
host = result.group(1)
a = FFmpeg(
executable='/opt/homebrew/bin/ffmpeg',
inputs={
inputs_path: '-user_agent "User-Agent: PostmanRuntime/7.28.3" -headers "Host: {host}"'.format(host=host)},
outputs={
outputs_path: '-c copy'}
)
a.run()
赏个🍗吧
0%