コード例 #1
0
ファイル: services.py プロジェクト: zptime/interact
def file_delete(f):
    if settings.USE_S3:
        if f.file_url:
            s3_storage.get_operator().delete(f.file_url)
    else:
        if f.file_url:
            file_storage.safe_delete(
                os.path.join(settings.BASE_DIR, f.file_url))
    f.is_del = TRUE_INT
    f.save()
コード例 #2
0
ファイル: services.py プロジェクト: zptime/interact
def voice_delete(voice):
    if settings.USE_S3:
        if voice.voice_url:
            s3_storage.get_operator().delete(voice.voice_url)
    else:
        if voice.voice_url:
            file_storage.safe_delete(
                os.path.join(settings.BASE_DIR, voice.voice_url))
    voice.is_del = TRUE_INT
    voice.save()
コード例 #3
0
ファイル: services.py プロジェクト: zptime/interact
def wx_voice_fetch(request, media_id, duration, access_token):
    voice_response = fetch(request.user, media_id, token=access_token)
    #HEAD:Content-disposition    attachment; filename="DTsH2SDUkNn12liTv53OuSXVhZ10yrWhwaPj4WlHJdqCBorq6U4-e4QefeA23Qr0.amr"
    pattern = r'attachment; filename="(.+)"'
    header_fname = voice_response.headers['Content-disposition']
    fname = re.findall(pattern, header_fname)[0]
    tmp_path = os.path.join(settings.TEMP_DIR, fname)
    with open(tmp_path, "wb") as tmp_f:
        for chunk in voice_response.iter_content(chunk_size=512):
            if chunk:
                tmp_f.write(chunk)
    response = trigger_upload_voice_request(request, fname, tmp_path, duration)
    status_code = response.status_code
    if status_code != 200:
        raise BusinessException(WX_UPLOAD_VOICE_FAIL)
    else:
        safe_delete(tmp_path)
    return response.text
コード例 #4
0
ファイル: services.py プロジェクト: zptime/interact
def video_delete(video):
    if settings.USE_S3:
        if video.video_url:
            s3_storage.get_operator().delete(video.video_url)
        if video.video_converted_url:
            s3_storage.get_operator().delete(video.video_converted_url)
        if video.video_snapshot_url:
            s3_storage.get_operator().delete(video.video_snapshot_url)
    else:
        if video.video_url:
            file_storage.safe_delete(
                os.path.join(settings.BASE_DIR, video.video_url))
        if video.video_converted_url:
            file_storage.safe_delete(
                os.path.join(settings.BASE_DIR, video.video_converted_url))
        if video.video_snapshot_url:
            file_storage.safe_delete(
                os.path.join(settings.BASE_DIR, video.video_snapshot_url))
    video.is_del = TRUE_INT
    video.save()
コード例 #5
0
ファイル: services.py プロジェクト: zptime/interact
def image_delete(image):
    if settings.USE_S3:
        if image.image_original_url:
            s3_storage.get_operator().delete(image.image_original_url)
        if image.image_thumb_url:
            s3_storage.get_operator().delete(image.image_thumb_url)
        if image.image_crop_url:
            s3_storage.get_operator().delete(image.image_crop_url)
    else:
        if image.image_original_url:
            file_storage.safe_delete(
                os.path.join(settings.BASE_DIR, image.image_original_url))
        if image.image_thumb_url:
            file_storage.safe_delete(
                os.path.join(settings.BASE_DIR, image.image_thumb_url))
        if image.image_crop_url:
            file_storage.safe_delete(
                os.path.join(settings.BASE_DIR, image.image_crop_url))
    image.is_del = TRUE_INT
    image.save()
コード例 #6
0
def convert(video_id):
    logger.info('convert and compress video %d ...' % video_id)
    CONVERT_TMP_PATH = 'video_convert'
    CONVERT_TMP_PATH_ABS = file_storage.safe_folder(
        os.path.join(settings.TEMP_DIR, CONVERT_TMP_PATH))
    video = SysVideo.objects.filter(id=video_id, is_del=FALSE_INT).first()
    if not video:
        return
    SysVideo.objects.filter(id=video_id, is_del=FALSE_INT).update(
        video_converted_status=VIDEO_CONVERT_STATUS_ING)

    media_path = settings.MEDIA_PATH_PROTECT if video.is_protected == TRUE_INT else settings.MEDIA_PATH_PUBLIC
    raw_fname = video.video_url[video.video_url.rfind('/') + 1:]
    converted_fname = raw_fname[:raw_fname.rfind('.')] + '_converted.mp4'
    # 转换后文件保存的相对路径
    relative_path = file_storage.gen_path(
        os.path.join(media_path, video.user_school.code, 'video'),
        converted_fname)

    # 截图相关信息
    SNAP_TMP_PATH = 'video_snapshot'
    SNAP_TMP_PATH_ABS = file_storage.safe_folder(
        os.path.join(settings.TEMP_DIR, SNAP_TMP_PATH))
    snap_fname = raw_fname[:raw_fname.rfind('.')] + '_snapshot.png'
    snap_relative_path = file_storage.gen_path(
        os.path.join(media_path, video.user_school.code, 'video_snapshot'),
        snap_fname)

    if settings.USE_S3:
        logger.info('download (s3) video %d to convert and compress' %
                    video_id)
        # 下载原始视频到本地临时目录
        _key = video.video_url
        _local_temp_path = os.path.join(CONVERT_TMP_PATH_ABS, raw_fname)
        _local_temp_path_after = os.path.join(CONVERT_TMP_PATH_ABS,
                                              converted_fname)

        _local_temp_path_snapshot = os.path.join(SNAP_TMP_PATH_ABS, snap_fname)

        s3_storage.get_operator().download_file(_key, _local_temp_path)

        # 转码生成新视频文件
        is_succ = tools.convert_and_compress(_local_temp_path,
                                             _local_temp_path_after)
        if is_succ:
            logger.info(
                'convert and compress video successfully, and start uploading to s3 ...'
            )
            raw_md5 = s3_storage.get_operator().upload_local_file(
                _local_temp_path_after, relative_path)
            logger.info('upload converted video to s3 successfully')
            SysVideo.objects.filter(id=video_id, is_del=FALSE_INT).update(
                video_converted_status=VIDEO_CONVERT_STATUS_SUCC,
                video_converted_url=relative_path,
            )
        else:
            logger.info('convert and compress video fail')
            SysVideo.objects.filter(id=video_id, is_del=FALSE_INT).update(
                video_converted_status=VIDEO_CONVERT_STATUS_FAIL)

        is_snap_succ = tools.video_snapshot(
            _local_temp_path_after, _local_temp_path_snapshot)  # 截图使用压缩后的视频文件
        if is_snap_succ:
            logger.info(
                'snapshot video successfully, and start uploading to s3 ...')
            raw_md5 = s3_storage.get_operator().upload_local_file(
                _local_temp_path_snapshot, snap_relative_path)
            logger.info('upload snapshot video to s3 successfully')
            snapshot_square = Image.open(_local_temp_path_snapshot).size
            SysVideo.objects.filter(id=video_id, is_del=FALSE_INT) \
                .update(video_snapshot_status=VIDEO_SNAPSHOT_STATUS_SUCC,
                        video_square='%d,%d' % (snapshot_square[0], snapshot_square[1]),
                        video_snapshot_url=snap_relative_path)
        else:
            logger.error('snapshot video fail')
            SysVideo.objects.filter(id=video_id, is_del=FALSE_INT) \
                .update(video_snapshot_status=VIDEO_SNAPSHOT_STATUS_FAIL)

        file_storage.safe_delete(_local_temp_path)
        file_storage.safe_delete(_local_temp_path_snapshot)
        file_storage.safe_delete(_local_temp_path_after)
    else:
        logger.info('use local video %d to convert and compress' % video_id)
        _local_temp_path_after = os.path.join(CONVERT_TMP_PATH_ABS,
                                              converted_fname)
        _local_temp_path_snapshot = os.path.join(SNAP_TMP_PATH_ABS, snap_fname)

        # 转码生成新视频文件
        is_succ = tools.convert_and_compress(
            os.path.join(settings.BASE_DIR, video.video_url),
            _local_temp_path_after)
        if is_succ:
            logger.info('convert and compress video successfully')
            shutil.copy(_local_temp_path_after,
                        os.path.join(settings.BASE_DIR, relative_path))
            SysVideo.objects.filter(id=video_id, is_del=FALSE_INT).update(
                video_converted_status=VIDEO_CONVERT_STATUS_SUCC,
                video_converted_url=relative_path,
            )
        else:
            logger.info('convert and compress video fail')
            SysVideo.objects.filter(id=video_id, is_del=FALSE_INT).update(
                video_converted_status=VIDEO_CONVERT_STATUS_FAIL)

        is_snap_succ = tools.video_snapshot(
            _local_temp_path_after, _local_temp_path_snapshot)  # 截图使用压缩后的视频文件
        if is_snap_succ:
            logger.info('snapshot video successfully')
            snapshot_square = Image.open(_local_temp_path_snapshot).size
            shutil.move(_local_temp_path_snapshot,
                        os.path.join(settings.BASE_DIR, snap_relative_path))
            SysVideo.objects.filter(id=video_id, is_del=FALSE_INT) \
                .update(video_square='%d,%d' % (snapshot_square[0], snapshot_square[1]),
                        video_snapshot_status=VIDEO_SNAPSHOT_STATUS_SUCC,
                        video_snapshot_url=relative_path)
        else:
            logger.info('snapshot video fail')
            SysVideo.objects.filter(id=video_id, is_del=FALSE_INT) \
                .update(video_snapshot_status=VIDEO_SNAPSHOT_STATUS_FAIL)
            file_storage.safe_delete(_local_temp_path_snapshot)
        file_storage.safe_delete(_local_temp_path_after)
コード例 #7
0
def convert_voice(voice_id):
    logger.info('convert voice %d ...' % voice_id)
    CONVERT_TMP_PATH = 'voice_convert'
    CONVERT_TMP_PATH_ABS = file_storage.safe_folder(
        os.path.join(settings.TEMP_DIR, CONVERT_TMP_PATH))
    voice = SysVoice.objects.filter(id=voice_id).first()
    if not voice:
        return
    voice.voice_converted_status = VOICE_CONVERT_STATUS_ING
    voice.save()
    media_path = settings.MEDIA_PATH_PROTECT if voice.is_protected == TRUE_INT else settings.MEDIA_PATH_PUBLIC
    raw_fname = voice.voice_url[voice.voice_url.rfind('/') + 1:]
    converted_fname = raw_fname[:raw_fname.rfind('.')] + '_converted.mp3'
    # 转换后文件保存的相对路径
    relative_path = file_storage.gen_path(
        os.path.join(media_path, voice.user_school.code, 'voice'),
        converted_fname)
    if settings.USE_S3:
        logger.info('download (s3) voice %d to convert' % voice_id)
        # 下载原始音频到本地临时目录
        _key = voice.voice_url
        _local_temp_path = os.path.join(CONVERT_TMP_PATH_ABS, raw_fname)
        _local_temp_path_after = os.path.join(CONVERT_TMP_PATH_ABS,
                                              converted_fname)
        s3_storage.get_operator().download_file(_key, _local_temp_path)
        # 转码生成新音频文件
        logger.info('convert voice %s to %s' %
                    (_local_temp_path, _local_temp_path_after))
        is_succ = tools.convert_voice_2_mp3(_local_temp_path,
                                            _local_temp_path_after)
        if is_succ:
            logger.info(
                'convert voice successfully, and start uploading to s3 ...')
            raw_md5 = s3_storage.get_operator().upload_local_file(
                _local_temp_path_after, relative_path)
            logger.info('upload converted voice to s3 successfully')
            voice.voice_converted_status = VOICE_CONVERT_STATUS_SUCC
            voice.voice_converted_url = relative_path
            voice.save()
        else:
            logger.info('convert voice fail')
            voice.voice_converted_status = VOICE_CONVERT_STATUS_FAIL
            voice.save()
        file_storage.safe_delete(_local_temp_path)
        file_storage.safe_delete(_local_temp_path_after)
    else:
        logger.info('use local voice %d to convert' % voice_id)
        _local_temp_path_after = os.path.join(CONVERT_TMP_PATH_ABS,
                                              converted_fname)
        # 转码生成新音频文件
        is_succ = tools.convert_voice_2_mp3(
            os.path.join(settings.BASE_DIR, voice.voice_url),
            _local_temp_path_after)
        if is_succ:
            logger.info('convert voice successfully')
            shutil.move(_local_temp_path_after,
                        os.path.join(settings.BASE_DIR, relative_path))
            voice.voice_converted_status = VOICE_CONVERT_STATUS_SUCC
            voice.voice_converted_url = relative_path
            voice.save()
        else:
            logger.info('convert voice fail')
            voice.voice_converted_status = VOICE_CONVERT_STATUS_FAIL
            voice.save()
            file_storage.safe_delete(_local_temp_path_after)