Python 文件中的 FFmpeg 已存在错误
FFmpeg in Python File already exists error
我正在开发一个使用 python(3.6) 和 Django(2.0) 的项目,我正在将视频转换为 mp4,如果它是任何其他格式的话。
这是我的代码:
来自 views.py:
def generate_thumbnail(filename, thumb_name):
print('func called')
print(filename)
video_input_path = os.path.join(filename)
img_output_path = os.path.join(thumb_name)
subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', 'vframes', '1', img_output_path])
def convert_to_mp4(video_name, only_name):
os.popen(
"ffmpeg -i '{input}' -ac 2 -b:v 2000k -c:a aac -c:v libx264 -b:a 160k -vprofile high -bf 0 -strict experimental -f mp4 '{output}.mp4'".format(
input=video_name, output=only_name))
return True
def perform_upload(video, thumbnail):
print('vdieo name is: {}'.format(video))
servise = discovery.build('storage', 'v1', credentials=credentials)
bucket_name = 'test_bucket004'
print('Uploading the video...')
media = MediaFileUpload(video, chunksize=4149304, mimetype='video/mp4',
resumable=True)
req = servise.objects().insert(
bucket=bucket_name,
name=str(video),
media_body=media,
body={"cacheControl": "public,max-age=31536000"},
predefinedAcl='publicRead'
)
resp = None
while resp is None:
status, resp = req.next_chunk()
print(resp)
video_url = 'http://storage.googleapis.com/' + bucket_name + '/' + str(video)
print('Uploading your thumbnail...')
media = MediaFileUpload(thumbnail, chunksize=4149304, mimetype='image/jpeg',
resumable=True)
req = servise.objects().insert(
bucket=bucket_name,
name=str(thumbnail),
media_body=media,
body={"cacheControl": "public,max-age=31536000"},
predefinedAcl='publicRead'
)
resp = None
while resp is None:
status, resp = req.next_chunk()
print(resp)
thumb_url = 'https://storage.googleapis.com/' + bucket_name + '/' + str(thumbnail)
return video_url, thumb_url
class VideoConverter(generics.ListCreateAPIView):
def get(self, request, *args, **kwargs):
return HttpResponse('Get request', status=200)
def post(self, request, *args, **kwargs):
serializer = VideoConverterSerializer(data=self.request.data)
validation = serializer.is_valid()
print(serializer.errors)
if validation is True:
url = request.POST.get('video_url')
filename = url.split('/')
filename = filename[-1]
print(filename)
ext = filename.split('.')
print(ext[-1])
only_name = ext[0]
urllib.request.urlretrieve(url, filename)
generate_thumbnail(filename, only_name + '_thumbnail.jpg')
if ext == 'mp4':
videourl, thumb_url = perform_upload(filename, only_name + '_thumbnail.jpg')
else:
conversion = convert_to_mp4(filename, only_name)
if conversion is True:
videourl, thumb_url = perform_upload(only_name + '.mp4', only_name + '_thumbnail.jpg')
return HttpResponse('Video url is: {}\n \nThumbnail url is: {}'.format(videourl, thumb_url))
else:
return HttpResponse('Not a valid request')
但是当我向它传递一个 Mp4 格式的视频时,它 returns 在 IDE 控制台中出现这样的错误:
ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers
built with Apple LLVM version 10.0.0 (clang-1000.10.43.1)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.0.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma
libavutil 56. 14.100 / 56. 14.100
libavcodec 58. 18.100 / 58. 18.100
libavformat 58. 12.100 / 58. 12.100
libavdevice 58. 3.100 / 58. 3.100
libavfilter 7. 16.100 / 7. 16.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 1.100 / 5. 1.100
libswresample 3. 1.100 / 3. 1.100
libpostproc 55. 1.100 / 55. 1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'PHP_GCS.mp4':
Metadata:
major_brand : mp42
minor_version : 1
compatible_brands: mp41mp42isom
creation_time : 2018-08-03T13:08:04.000000Z
Duration: 00:01:21.40, start: 0.000000, bitrate: 1584 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1918x1078 [SAR 1:1 DAR 137:77], 1581 kb/s, 30 fps, 30 tbr, 600 tbn, 1200 tbc (default)
Metadata:
creation_time : 2018-08-03T13:08:04.000000Z
handler_name : Core Media Video
File 'PHP_GCS.mp4' already exists. Overwrite ? [y/N]
并在此处停止执行,直到我按下回车 button.I 我真的很困惑为什么这很开心,因为当视频已经是 mp4 时我没有使用 ffmpeg
但仅用于缩略图生成.
这里有什么问题吗?
提前致谢!
我认为您在代码中遗漏了一些内容。
视频转换器中的 Post 处理程序具有此代码:
ext = filename.split('.')
print(ext[-1])
only_name = ext[0]
urllib.request.urlretrieve(url, filename)
generate_thumbnail(filename, only_name + '_thumbnail.jpg')
if ext == 'mp4': # mistake here
但您没有检查 if 语句中的正确部分。
你应该写 if ext[-1] == 'mp4'
因为现在你正在比较一个列表和一个总是 return false 的字符串!
这可能会解决您的问题,如果不能,请告诉我,我会相应地更新我的答案。
我正在开发一个使用 python(3.6) 和 Django(2.0) 的项目,我正在将视频转换为 mp4,如果它是任何其他格式的话。
这是我的代码:
来自 views.py:
def generate_thumbnail(filename, thumb_name):
print('func called')
print(filename)
video_input_path = os.path.join(filename)
img_output_path = os.path.join(thumb_name)
subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', 'vframes', '1', img_output_path])
def convert_to_mp4(video_name, only_name):
os.popen(
"ffmpeg -i '{input}' -ac 2 -b:v 2000k -c:a aac -c:v libx264 -b:a 160k -vprofile high -bf 0 -strict experimental -f mp4 '{output}.mp4'".format(
input=video_name, output=only_name))
return True
def perform_upload(video, thumbnail):
print('vdieo name is: {}'.format(video))
servise = discovery.build('storage', 'v1', credentials=credentials)
bucket_name = 'test_bucket004'
print('Uploading the video...')
media = MediaFileUpload(video, chunksize=4149304, mimetype='video/mp4',
resumable=True)
req = servise.objects().insert(
bucket=bucket_name,
name=str(video),
media_body=media,
body={"cacheControl": "public,max-age=31536000"},
predefinedAcl='publicRead'
)
resp = None
while resp is None:
status, resp = req.next_chunk()
print(resp)
video_url = 'http://storage.googleapis.com/' + bucket_name + '/' + str(video)
print('Uploading your thumbnail...')
media = MediaFileUpload(thumbnail, chunksize=4149304, mimetype='image/jpeg',
resumable=True)
req = servise.objects().insert(
bucket=bucket_name,
name=str(thumbnail),
media_body=media,
body={"cacheControl": "public,max-age=31536000"},
predefinedAcl='publicRead'
)
resp = None
while resp is None:
status, resp = req.next_chunk()
print(resp)
thumb_url = 'https://storage.googleapis.com/' + bucket_name + '/' + str(thumbnail)
return video_url, thumb_url
class VideoConverter(generics.ListCreateAPIView):
def get(self, request, *args, **kwargs):
return HttpResponse('Get request', status=200)
def post(self, request, *args, **kwargs):
serializer = VideoConverterSerializer(data=self.request.data)
validation = serializer.is_valid()
print(serializer.errors)
if validation is True:
url = request.POST.get('video_url')
filename = url.split('/')
filename = filename[-1]
print(filename)
ext = filename.split('.')
print(ext[-1])
only_name = ext[0]
urllib.request.urlretrieve(url, filename)
generate_thumbnail(filename, only_name + '_thumbnail.jpg')
if ext == 'mp4':
videourl, thumb_url = perform_upload(filename, only_name + '_thumbnail.jpg')
else:
conversion = convert_to_mp4(filename, only_name)
if conversion is True:
videourl, thumb_url = perform_upload(only_name + '.mp4', only_name + '_thumbnail.jpg')
return HttpResponse('Video url is: {}\n \nThumbnail url is: {}'.format(videourl, thumb_url))
else:
return HttpResponse('Not a valid request')
但是当我向它传递一个 Mp4 格式的视频时,它 returns 在 IDE 控制台中出现这样的错误:
ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers built with Apple LLVM version 10.0.0 (clang-1000.10.43.1) configuration: --prefix=/usr/local/Cellar/ffmpeg/4.0.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma libavutil 56. 14.100 / 56. 14.100 libavcodec 58. 18.100 / 58. 18.100 libavformat 58. 12.100 / 58. 12.100 libavdevice 58. 3.100 / 58. 3.100 libavfilter 7. 16.100 / 7. 16.100 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 1.100 / 5. 1.100 libswresample 3. 1.100 / 3. 1.100 libpostproc 55. 1.100 / 55. 1.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'PHP_GCS.mp4': Metadata: major_brand : mp42 minor_version : 1 compatible_brands: mp41mp42isom creation_time : 2018-08-03T13:08:04.000000Z Duration: 00:01:21.40, start: 0.000000, bitrate: 1584 kb/s Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1918x1078 [SAR 1:1 DAR 137:77], 1581 kb/s, 30 fps, 30 tbr, 600 tbn, 1200 tbc (default) Metadata: creation_time : 2018-08-03T13:08:04.000000Z handler_name : Core Media Video
File 'PHP_GCS.mp4' already exists. Overwrite ? [y/N]
并在此处停止执行,直到我按下回车 button.I 我真的很困惑为什么这很开心,因为当视频已经是 mp4 时我没有使用 ffmpeg
但仅用于缩略图生成.
这里有什么问题吗?
提前致谢!
我认为您在代码中遗漏了一些内容。
视频转换器中的 Post 处理程序具有此代码:
ext = filename.split('.')
print(ext[-1])
only_name = ext[0]
urllib.request.urlretrieve(url, filename)
generate_thumbnail(filename, only_name + '_thumbnail.jpg')
if ext == 'mp4': # mistake here
但您没有检查 if 语句中的正确部分。
你应该写 if ext[-1] == 'mp4'
因为现在你正在比较一个列表和一个总是 return false 的字符串!
这可能会解决您的问题,如果不能,请告诉我,我会相应地更新我的答案。