【发布时间】:2016-02-12 02:42:37
【问题描述】:
我为 ffmpeg 编写了一个测试演示,读取了一个 flac 文件并将其解码为一个新的 pcm 文件。
完成工作后,我找不到输出文件。
所以我用新的FILE 指针打开输出文件,打开没问题,有没有人给点提示,谢谢。
static int decode_packet(int *got_frame, int cached)
{
int ret = 0;
int decoded = pkt.size;
*got_frame = 0;
if (pkt.stream_index == audio_stream_idx) {
/* decode audio frame */
ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt);
if (ret < 0) {
LOG("Error decoding audio frame (%s)\n", av_err2str(ret));
return ret;
}
decoded = FFMIN(ret, pkt.size);
if (*got_frame) {
LOG("frame->format: %d", frame->format);
size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16/*frame->format*/);
LOG("audio_frame%s n:%d nb_samples:%d pts:%s\n", cached ? "(cached)" : "", audio_frame_count++, frame->nb_samples,
av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));
fwrite(frame->extended_data[0], 1, unpadded_linesize, audio_dst_file);
}
}
if (*got_frame && api_mode == API_MODE_NEW_API_REF_COUNT)
av_frame_unref(frame);
return decoded;
}
// as convient i write the abspath `/storage/emulated/0` which i got by rountine Environment.getExternalStorageDirectory().getAbsolutePath()
const char *src_filename = "/storage/emulated/0/pyghlkn.flac";
const char *audio_dst_filename = "/storage/emulated/0/test.pcm";
FILE *audio_dst_file = NULL;
FILE *audio_dump = NULL;
JNIEXPORT void JNICALL Java_cn_com_longmaster_ffmpeg_encoder
(JNIEnv *, jobject)
{
//......
/* read frames from the file */
while (av_read_frame(fmt_ctx, &pkt) >= 0) {
AVPacket orig_pkt = pkt;
do {
ret = decode_packet(&got_frame, 0);
LOG("write decode_packet... pkt.size: %d ", pkt.size);
if (ret < 0)
break;
pkt.data += ret;
pkt.size -= ret;
} while (pkt.size > 0);
av_free_packet(&orig_pkt);
}
/* flush cached frames */
pkt.data = NULL;
pkt.size = 0;
do {
decode_packet(&got_frame, 1);
LOG("flush cached frames");
} while (got_frame);
if (audio_dump)
{
LOG("default audio_dump ready...");
}
else
{
LOG("default audio_dump not ready...");
}
audio_dump = fopen(audio_dst_filename, "rb");
if (audio_dump) {
LOG("open pcm file ok...");
} else {
LOG("open pcm file fail...");
}
avcodec_close(audio_dec_ctx);
avformat_close_input(&fmt_ctx);
if (audio_dst_file) {
fclose(audio_dst_file);
}
av_frame_free(&frame);
return ;
}
【问题讨论】:
-
我找不到输出文件 - 你是怎么找的?你试过
adb shell吗? -
另外,通过 Android API 获取外部存储路径(或您想要存储文件的任何位置)并将路径传递给您的本机代码可能是一个更好的主意,而不是使用像
/storage/emulated/0这样的硬编码路径。 -
我终于通过第三部分APP得到了文件,并把它复制到一个新的目录中,然后将手机用作U盘。
标签: android c ffmpeg java-native-interface fopen